2009-09-03 100 views

回答

12

一般来说,铸造指的是一个明确的转换,无论它是由C样式转换(T(v)(T)v)或C++做 - 格调铸(static_castconst_castdynamic_cast,或reinterpret_cast)。转换通常是用于任何时间的变量转换为另一种更通用的术语:

std::string s = "foo"; // Conversion from char[] to char* to std::string 
int i = 4.3; // Conversion from float to int 
float *f = reinterpret_cast<float*>(&i); // (illegal) conversion from int* to float* 
+0

所以没有区别 – 2009-09-04 08:51:00

+0

不同之处在于cast是* explicit *。 C++关键字可以被grep化。 C和C++都演示了转换是有目的地完成的,并且得到程序员的同意。隐含的转换可能是有意的,或者是错误的。 – DevSolar 2009-09-08 10:52:15

0

当您使用字符串时,会出现一个主要差异。你不能说(int)“234”并且得到整数234.类型转换通常只对原始数字数据类型起作用。

2

类型转换意味着你采取的比特串和不同的方式解释它们。类型转换意味着您可以将一个位的字符串从一个上下文中有用的配置转换为另一个有用的配置。

例如,假设我写

int x=65; 
char c=(char) x; 
char* s=(char*) x; 

C现在将包含字符“A”,因为如果我重新诠释十进制数65为一个字符,我得到的字母“A”。 s现在将成为一个指向驻留在内存位置65的字符串的指针。这几乎肯定是无用的事情,因为我不知道该内存位置处的内容。

itoa(x, s, 10); 

是一种类型转换。这应该给我的字符串“65”。

也就是说,对于剧组,我们仍然在看同一个内存位置。我们只是以不同的方式解释数据。通过转换,我们正在生成从旧数据中导出的新数据,但它与旧数据不一样。

+0

所以你说,'reinterpret_cast'执行转换,但'static_cast'(尽管它的名字)执行转换?我会发现最令人困惑的。 – jogojapan 2012-11-22 05:37:01

1

型铸件可以做转换的最低金额:

signed char Schar; // 1 byte (8 bits) to hold 256 values: -128 to 127 
unsigned char Uchar; // 1 byte (8 bits) to hold 256 values: 0 to 255 
... 
if (Schar < -10 ) ... // compiler uses SIGNED comparision 
Uchar = Schar; // implicit conversion only copies the 8 bits 
Uchar = (char) Schar; // explicit conversion may be required by compiler 
if (Uchar > 200) ... // compiler uses UNSIGNED comparision 
...OR... 
if ((unsigned char) Schar > 200) ... // explicit conversion for UNSIGNED comparision 

short Sshort; // 2 bytes (16 bits) to hold 65536 values: -32768 to 32767 
unsigned short Ushort; // 2 bytes (16 bits) to hold 65536 values: 0 to 65536 
... 
// when moving 8 bits into 16 bit variables, what to do with other 8 bits ? 
Sshort = (signed short) Uchar; // move 8 bits over and use 0s for other 8 bits 
Sshort = (signed short) Schar; // same, but use 1s if negative to make Sshort negative 

但是,这可能会被视为类型转换:

float dbl; // 4 bytes to store floating number in IEEE format 
long lng; // 4 bytes to store 32 bit integer value in 2's complement format 
... 
dbl = lng; // convert from 2's comp to IEEE format - all bits change ! 
dbl = (float) lng; // explicit form 

注:int通常是一样的依赖于编译器shortlong/CPU 注意:signed通常是可选的,因为这通常是默认的

不会发生任何转换,当您指定的所有变量占用相同的内存空间:

typedef union MYUNION // all members occupy same space (memory bytes) 
{ 
    signed char Schar; // usual default for char 
    unsigned char Uchar; 
    signed short Sshort; // usual default for short 
    unsigned short Ushort; 
    signed long Slong; // usual default for long 
    unsigned long Ulong; 
    float flt; 
    double dbl; 
}; 

MYUNION myunion; 

myunion.Schar = ... // set variable (memory byte) to value 

if ((unsigned char) myunion.Schar > 200) ... // unsigned compare works ok 
... is same as (also without moving any data around) ... 
if (myunion.Uchar > 200) ... // unsigned compare works ok 

... myunion.Sshort ... // other 8 bits are UNASSIGNED GARBAGE ! 

myunion.Sshort = myunion.Schar; // provide all 16 bits from Schar 
... myunion.Sshort ... // Sshort of valid now 

myunion.dbl = 12345.0; 
... myunion.Ulong ... // has weird value from odd IEEE bit format 

myunion.Ulong = (unsigned long) myunion.dbl; // do explicit conversion 
... myunion.Ulong ... // has CONVERTED 12345 value 

注:*(unsigned long*)&dbl也产生怪异的值。它确实: a)获取double dbl的地址(位的位置和字节) b)将地址视为无符号长地址 c)从该位置获得无符号long 当然,有一些实际的应用程序这种技术。例子:解析一个复杂的外部二进制文件或在512字节内存的CPU上,等等。