重新解释类型转换和c样式转换之间的c ++区别

重新解释类型转换和c样式转换之间的c ++区别

问题描述:

代码:

char keyStr[50]={ 0x5F, 0x80 /* bla bla */ };
uint32_t* reCast  = reinterpret_cast< uint32_t* >( &keyStr[29] );
uint32_t* reCast2 = ( uint32_t* )&keyStr[29];
if( reCast == reCast2 ){
    cout << "Same Thing!";
}

输出:

同一件事!

Same Thing!

我想知道两种铸造方法之间有什么区别. 同样,如果您可以(举例说明)指定static_cast,dynamic_cast和其他类型的转换之间的区别(即,在保持较低级别并尽可能接近汇编语言的情况下).

I wonder what's the difference between the two casting methods. Also if you could specify ( with examples ) difference between static_cast, dynamic_cast, and other types of casting you know ( i.e. while staying as low level and as close to assembly language as possible ).

static_cast
dynamic_cast
const_cast
reinterpret_cast
C-style cast (type)value
Function-style cast type(value)

谢谢.

请阅读P.S. 我从上面的示例中知道,reinterpret_cast将int指针分配给keyStr [29]的地址. 在汇编中将转换为:

Please read the P.S. I know from the example above that reinterpret_cast assigns to the int pointer the address of keyStr[29] In assembly that would translate into:

lea eax, [keyStr+1D]
mov [reCast], eax

因此,换句话说,reprepare_cast在较低的预期水平上完全没有危险,因为它不会修改原始数据.

So in other words reinterpret_cast, in a low level prospective, is not dangerous at all as it does not modify the original data.

我想知道其他铸造方法在低水平方面的表现. 因此,例如,低级对象只是一个保存地址的变量. 以及该对象的类型是编译器随后如何解释该地址以及它如何偏移该地址.(这正是我在组装中不感兴趣的东西,我不太关心该变量是否包含值,指针或指针.对象(即另一个指针)). 另一件事可能是相同的,就是int和int *之间的区别,或者unsigned int和int之间的区别;所有4个声明都生成相同的汇编指令. (推送值)或(sub esp-(int的长度)&& mov esp,值) 我希望这可以弄清楚这个问题,以及为什么我将其标记为低级代码"和汇编"

I wanted to know how the other casting methods behave in a low level way. So, for example, an object, in a low level way, is just a variable which holds an address. And the type if that object is how the compiler then interprets that address and how it offsets it.( this is exactly what I'm not interested in, in assembly, i could care less if that variable holds a value, a pointer or an object ( i.e. another pointer ) ). Another thing that could be just the same, is the difference between int and int* or unsigned int and int; all 4 declarations generate the same assembly instruction. ( push value ) or (sub esp-(length of int) && mov esp, value) I hope this clarifies the question and why I tagged it "low-level-code" and "assembly"

P.S.在这个程序中,我试图创建我不关心非可移植性或其他高级内容的东西.我正在尝试尽可能降低级别,并尽可能接近汇编语言.这意味着,对于该程序,内存只是内存(即0和1位),类型并不重要(例如,我不在乎内存地址:0x123是"int"类型还是"float"类型,它只是数据")

P.S. In this program I'm trying to create I don't care for non portability or other high level stuff. I'm trying to be as low level as possible and as close to assembly language as possible. That means that, for this program, memory is just memory ( i.e. 0 and 1 bits ) and types are not important ( e.g. I don't care if mem address: 0x123 is an "int" type or "float" type, it's just "data")

reinterpret_castconst_cast是解决C ++类型系统的方法.正如您对reinterpret_cast所指出的那样,这通常转化为很少或没有汇编代码.

reinterpret_cast and const_cast are ways of getting around the C++ type system. As you noted for reinterpret_cast, this usually translates to little or no assembly code.

static_cast最尊重C ++类型系统.它可以将数字从一种类型转换为另一种类型,或调用构造函数或调用转换函数.或对于派生到基本的转换,可能涉及在vtable中添加字节偏移量和/或查找. static_cast还可以通过将指针或引用从非虚拟基类型下垂"到派生类型(可能减去字节偏移)来弯曲类型系统的规则.

static_cast mostly respects the C++ type system. It could convert a number from one type to another, or call a constructor, or call a conversion function. Or for a derived-to-base conversion, it might involve adding byte offsets and/or lookups into a vtable. static_cast can also bend the type system's rules by "downcasting" a pointer or reference from a non-virtual base type to a derived type, possibly subtracting a byte offset.

然后是指向成员的指针.它们可能不在这里,但是static_cast或多或少地对它们执行了类似于类指针转换的操作.

And then there are pointers-to-member. They're probably beside the point here, but static_cast does things to them more or less analogous to class pointer conversions.

dynamic_cast更加严格地尊重C ++类型系统.它以有用的形式在运行时检查指针/引用是否实际上指向/引用了指定类型的对象.通常会在后台调用魔术库函数.

dynamic_cast respects the C++ type system even more strictly. In its useful form, it checks at runtime whether or not a pointer/reference actually points/refers to an object of the specified type. It typically calls a magic library function under the covers.

带有一个参数的函数样式强制转换与C样式强制转换具有完全相同的效果. (具有多个参数,函数样式的强制转换必须是使用类构造函数的临时初始化.)C样式的强制转换从以下列表中进行了有意义的第一件事:

A function-style cast with one argument has exactly the same effect as a C-style cast. (With more than one argument, a function-style cast must be a temporary initialization using a class constructor.) A C-style cast does the first thing that makes sense out of the following list:

  • 一个const_cast
  • 一个static_cast
  • 一个static_cast然后一个const_cast
  • a reinterpret_cast
  • 一个reinterpret_cast然后一个const_cast
  • a const_cast
  • a static_cast
  • a static_cast and then a const_cast
  • a reinterpret_cast, or
  • a reinterpret_cast and then a const_cast

一个例外:C样式转换可以忽略类之间的私有继承关系和受保护继承关系,假装它们具有公共继承关系.

One exception: C-style casts can ignore private and protected inheritance relations between classes, pretending they have a public inheritance relation instead.

C样式强制转换通常在C ++中不是首选,因为它对要发生的事情不太明确.

C-style casts are usually not preferred in C++ because it's less specific about what you want to happen.