2009-09-03 63 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上,等等。