【发布时间】:2009-09-03 15:58:27
【问题描述】:
c++中的类型转换和类型转换有什么区别吗?
【问题讨论】:
标签: c++ type-conversion casting
c++中的类型转换和类型转换有什么区别吗?
【问题讨论】:
标签: c++ type-conversion casting
通常,转换是指显式转换,无论是通过 C 样式转换(T(v) 或 (T)v)还是 C++ 样式转换(static_cast、const_cast、dynamic_cast 或 @987654326 @)。转换通常是一个更通用的术语,用于将变量转换为另一个变量:
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*
【讨论】:
类型转换意味着您获取一串位并以不同的方式解释它们。类型转换意味着您将一串位从在一个上下文中有用的配置转换为在另一个上下文中有用的配置。
例如,假设我写
int x=65;
char c=(char) x;
char* s=(char*) x;
c 现在将包含字符“A”,因为如果我将十进制数 65 重新解释为字符,我会得到字母“A”。 s 现在将是一个指向位于内存位置 65 的字符串的指针。这几乎可以肯定是一件没用的事情,因为我不知道那个内存位置是什么。
itoa(x, s, 10);
是一种类型转换。那应该给我字符串“65”。
也就是说,我们仍然在寻找相同的内存位置。我们只是以不同的方式解释那里的数据。通过转换,我们生成了从旧数据派生的新数据,但它与旧数据不同。
【讨论】:
reinterpret_cast 执行转换,但static_cast(尽管它的名字)执行转换?我会觉得这最令人困惑。
使用字符串时会出现主要差异之一。您不能说 (int)"234" 并获得整数 234。类型转换通常仅适用于原始数字数据类型。
【讨论】:
类型转换可以做最少的转换:
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 通常与 short 或 long 相同,具体取决于编译器/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) 获取双 dbl 的地址(位和字节的位置)
b) 将地址视为无符号长整数的地址
c) 从该位置获取 unsigned long
当然,这种技术也有一些实际应用。示例:解析复杂的外部二进制文件或在 512 字节内存的 CPU 上等
【讨论】: