类型铸造和类型转换之间有什么区别吗?

时间:2021-09-15 20:05:29

Is there any difference between type casting & type conversion in c++.

在c++中,类型铸造和类型转换有什么区别吗?

4 个解决方案

#1


13  

Generally, casting refers to an explicit conversion, whether it's done by C-style cast (T(v) or (T)v) or C++-style cast (static_cast, const_cast, dynamic_cast, or reinterpret_cast). Conversion is generally a more generic term used for any time a variable is converted to another:

通常,强制转换指的是显式转换,无论是C型强制转换(T(v)还是(T)v)或c++强制转换(static_cast、const_cast、dynamic_cast或reinterpretation t_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*

#2


2  

Type casting means that you take a string of bits and interpret them differently. Type conversion means that you transform a string of bits from a configuration useful in one context to a configuration useful in another.

类型转换意味着您取一串位并以不同的方式解释它们。类型转换意味着您将从一个配置中使用的一些位元转换为在另一个环境中有用的配置。

For example, suppose I write

例如,假设我写作

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

c will now contain the character 'A', because if I reinterpret the decimal number 65 as a character, I get the letter 'A'. s will now be a pointer to a character string residing at memory location 65. This is almost surely a useless thing to do, as I have no idea what is at that memory location.

c将包含字符“A”,因为如果我将小数65重新解释为字符,就会得到字母“A”。s现在将成为驻留在内存位置65中的字符串的指针。这几乎肯定是一件无用的事情,因为我不知道那个内存位置是什么。

itoa(x, s, 10);

is a type conversion. That should give me the string "65".

是一个类型转换。这应该是字符串"65"

That is, with casts we are still looking at the same memory location. We are just interpreting the data there differently. With conversions we are producing new data that is derived from the old data, but it is not the same as the old data.

也就是说,使用强制类型转换,我们仍然在寻找相同的内存位置。我们只是用不同的方式解释数据。通过转换,我们产生了从旧数据派生的新数据,但它与旧数据不同。

#3


1  

One of the major differences occurs when you work with strings. You cannot say (int)"234" and get the integer 234. Type casting generally only works on primitive numeric data types.

当处理字符串时,会出现一个主要的区别。你不能说(int)“234”,得到整数234。类型转换通常只适用于原始的数字数据类型。

#4


1  

Type casting may do a minimum amount of conversion:

类型铸造可以做最小数量的转换:

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

But this might be considered Type Conversion:

但这可能被认为是类型转换:

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

NOTE: int is usually the same as short or long depending on compiler/CPU NOTE: signed is usually optional as this is often the default

注意:根据编译器/CPU的不同,int通常等同于短或长。注意:签名通常是可选的,因为这通常是默认值

No conversion takes place when you specify all variables occupy the same memory space:

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

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

NOTE: *(unsigned long*)&dbl also produces weird values. It does: a) take address (location of the bits and bytes) of double dbl b) consider the address as an address of an unsigned long c) get unsigned long from that location Of course, there are some real applications of this technique. Examples: parsing a complicated external binary file or on CPUs with 512 bytes of memory, etc.

注意:*(unsigned long*)和dbl也会产生奇怪的值。它是这样的:a)取双dbl b的地址(位和字节的位置)考虑地址作为一个无符号长c的地址)从那个位置得到无符号长当然,这个技术有一些实际的应用。示例:解析复杂的外部二进制文件或具有512字节内存的cpu等。

#1


13  

Generally, casting refers to an explicit conversion, whether it's done by C-style cast (T(v) or (T)v) or C++-style cast (static_cast, const_cast, dynamic_cast, or reinterpret_cast). Conversion is generally a more generic term used for any time a variable is converted to another:

通常,强制转换指的是显式转换,无论是C型强制转换(T(v)还是(T)v)或c++强制转换(static_cast、const_cast、dynamic_cast或reinterpretation t_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*

#2


2  

Type casting means that you take a string of bits and interpret them differently. Type conversion means that you transform a string of bits from a configuration useful in one context to a configuration useful in another.

类型转换意味着您取一串位并以不同的方式解释它们。类型转换意味着您将从一个配置中使用的一些位元转换为在另一个环境中有用的配置。

For example, suppose I write

例如,假设我写作

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

c will now contain the character 'A', because if I reinterpret the decimal number 65 as a character, I get the letter 'A'. s will now be a pointer to a character string residing at memory location 65. This is almost surely a useless thing to do, as I have no idea what is at that memory location.

c将包含字符“A”,因为如果我将小数65重新解释为字符,就会得到字母“A”。s现在将成为驻留在内存位置65中的字符串的指针。这几乎肯定是一件无用的事情,因为我不知道那个内存位置是什么。

itoa(x, s, 10);

is a type conversion. That should give me the string "65".

是一个类型转换。这应该是字符串"65"

That is, with casts we are still looking at the same memory location. We are just interpreting the data there differently. With conversions we are producing new data that is derived from the old data, but it is not the same as the old data.

也就是说,使用强制类型转换,我们仍然在寻找相同的内存位置。我们只是用不同的方式解释数据。通过转换,我们产生了从旧数据派生的新数据,但它与旧数据不同。

#3


1  

One of the major differences occurs when you work with strings. You cannot say (int)"234" and get the integer 234. Type casting generally only works on primitive numeric data types.

当处理字符串时,会出现一个主要的区别。你不能说(int)“234”,得到整数234。类型转换通常只适用于原始的数字数据类型。

#4


1  

Type casting may do a minimum amount of conversion:

类型铸造可以做最小数量的转换:

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

But this might be considered Type Conversion:

但这可能被认为是类型转换:

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

NOTE: int is usually the same as short or long depending on compiler/CPU NOTE: signed is usually optional as this is often the default

注意:根据编译器/CPU的不同,int通常等同于短或长。注意:签名通常是可选的,因为这通常是默认值

No conversion takes place when you specify all variables occupy the same memory space:

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

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

NOTE: *(unsigned long*)&dbl also produces weird values. It does: a) take address (location of the bits and bytes) of double dbl b) consider the address as an address of an unsigned long c) get unsigned long from that location Of course, there are some real applications of this technique. Examples: parsing a complicated external binary file or on CPUs with 512 bytes of memory, etc.

注意:*(unsigned long*)和dbl也会产生奇怪的值。它是这样的:a)取双dbl b的地址(位和字节的位置)考虑地址作为一个无符号长c的地址)从那个位置得到无符号长当然,这个技术有一些实际的应用。示例:解析复杂的外部二进制文件或具有512字节内存的cpu等。