类型铸造& C++中的类型转换是否有区别。类型转换和类型转换是否有区别?
回答
一般来说,铸造指的是一个明确的转换,无论它是由C样式转换(T(v)
或(T)v
)或C++做 - 格调铸(static_cast
,const_cast
,dynamic_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*
当您使用字符串时,会出现一个主要差异。你不能说(int)“234”并且得到整数234.类型转换通常只对原始数字数据类型起作用。
类型转换意味着你采取的比特串和不同的方式解释它们。类型转换意味着您可以将一个位的字符串从一个上下文中有用的配置转换为另一个有用的配置。
例如,假设我写
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'(尽管它的名字)执行转换?我会发现最令人困惑的。 – jogojapan 2012-11-22 05:37:01
型铸件可以做转换的最低金额:
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)获取double dbl的地址(位的位置和字节) b)将地址视为无符号长地址 c)从该位置获得无符号long 当然,有一些实际的应用程序这种技术。例子:解析一个复杂的外部二进制文件或在512字节内存的CPU上,等等。
- 1. 类型转换和类型转换之间的区别?
- 2. 转换类型
- 3. 类型转换++
- 4. 转换类型
- 5. 类型转换
- 6. 转换类型
- 7. 类型铸造和类型转换
- 8. C#泛型类型转换Decimal转换为Float - “不能转换类型'T1'? 'T2'“
- 9. malloc中是否需要类型转换?
- 10. 类类型转换:为什么类型转换功能不叫
- 11. c-namespace和类型转换
- 12. 类型转换和指针
- 13. 指针和类型转换
- 14. 类型转换和foreach
- 15. boost :: optional和类型转换
- 16. 铸造类型和转换
- 17. jOOQ转换器和类型
- 18. C#类型转换
- 19. Java类型转换
- 20. 列类型转换
- 21. Groovy类型转换
- 22. Regd。类型转换
- 23. SPARQL类型转换?
- 24. 类型转换C++
- 25. sqlite3_column_text()类型转换
- 26. Javascript类型转换
- 27. C#类型转换
- 28. C#类型转换?
- 29. Int类型转换
- 30. C++类型转换
所以没有区别 – 2009-09-04 08:51:00
不同之处在于cast是* explicit *。 C++关键字可以被grep化。 C和C++都演示了转换是有目的地完成的,并且得到程序员的同意。隐含的转换可能是有意的,或者是错误的。 – DevSolar 2009-09-08 10:52:15