以下各项的正确用途是什么:

静态铸造动态铸造常量_成本重新解释(_C)(type)值(C样式转换)类型(值)(函数样式转换)

如何决定在哪些特定情况下使用哪一种?


当前回答

这能回答你的问题吗?

我从来没有使用过reinterpret_cast,我想知道遇到一个需要它的案例是否不是糟糕设计的味道。在代码库中,我使用了很多dynamic_cast。与static_cast的区别在于dynamic_cast执行运行时检查,这可能(更安全),也可能(开销更大)是您想要的(请参见msdn)。

其他回答

(上面给出了大量的理论和概念解释)

下面是我使用static_cast、dynamic_cast、const_cast、reinterpret_cast时的一些实际示例。

(也可参考此来理解解释:http://www.cplusplus.com/doc/tutorial/typecasting/)

静态铸造:

OnEventData(void* pData)

{
  ......

  //  pData is a void* pData, 

  //  EventData is a structure e.g. 
  //  typedef struct _EventData {
  //  std::string id;
  //  std:: string remote_id;
  //  } EventData;

  // On Some Situation a void pointer *pData
  // has been static_casted as 
  // EventData* pointer 

  EventData *evtdata = static_cast<EventData*>(pData);
  .....
}

dynamic_cast:

void DebugLog::OnMessage(Message *msg)
{
    static DebugMsgData *debug;
    static XYZMsgData *xyz;

    if(debug = dynamic_cast<DebugMsgData*>(msg->pdata)){
        // debug message
    }
    else if(xyz = dynamic_cast<XYZMsgData*>(msg->pdata)){
        // xyz message
    }
    else/* if( ... )*/{
        // ...
    }
}

常量成本(_C):

// *Passwd declared as a const

const unsigned char *Passwd


// on some situation it require to remove its constness

const_cast<unsigned char*>(Passwd)

重新解释(_C):

typedef unsigned short uint16;

// Read Bytes returns that 2 bytes got read. 

bool ByteBuffer::ReadUInt16(uint16& val) {
  return ReadBytes(reinterpret_cast<char*>(&val), 2);
}

reinterpret_cast的好特性(其他答案中没有提到)是它允许我们为函数类型创建一种void*指针。通常,对于对象类型,使用static_cast检索存储在void*中的指针的原始类型:

  int i = 13;
  void *p = &i;
  auto *pi = static_cast<int*>(p);

对于函数,我们必须使用reinterpret_cast两次:

#include<iostream>

using any_fcn_ptr_t = void(*)();


void print(int i)
{
   std::cout << i <<std::endl;
}

int main()
{     
  //Create type-erased pointer to function:
  auto any_ptr = reinterpret_cast<any_fcn_ptr_t>(&print);
  
  //Retrieve the original pointer:
  auto ptr = reinterpret_cast< void(*)(int) >(any_ptr);
  
  ptr(7);
}

使用reinterpret_cast,我们甚至可以为指向成员函数的指针获得类似类型的void*指针。

与普通的void*和static_cast一样,C++保证ptr指向print函数(只要我们将正确的类型传递给reinterpret_cast)。

为了理解,让我们考虑下面的代码片段:

struct Foo{};
struct Bar{};

int main(int argc, char** argv)
{
    Foo* f = new Foo;

    Bar* b1 = f;                              // (1)
    Bar* b2 = static_cast<Bar*>(f);           // (2)
    Bar* b3 = dynamic_cast<Bar*>(f);          // (3)
    Bar* b4 = reinterpret_cast<Bar*>(f);      // (4)
    Bar* b5 = const_cast<Bar*>(f);            // (5)

    return 0;
}

只有第(4)行编译没有错误。只能使用reinterpret_cast将指向对象的指针转换为指向任何无关对象类型的指针。

需要注意的一点是:dynamic_cast在运行时会失败,但在大多数编译器上,它也会编译失败,因为被转换的指针的结构中没有虚拟函数,这意味着dynamic_cast只能与多态类指针一起工作。

何时使用C++转换:

使用static_cast等效于进行值转换的C样式转换,或者当我们需要将指针从类显式上转换到其超类时。使用const_cast删除const限定符。使用reinterpret_cast执行指针类型与整数和其他指针类型之间的不安全转换。只有当我们知道自己在做什么并且了解别名问题时,才能使用此选项。

虽然其他答案很好地描述了C++转换之间的所有差异,但我想补充一点,为什么不应该使用C样式转换(Type)var和Type(var)。

对于C++初学者来说,C风格的强制转换看起来像是C++强制转换的超集操作(static_cast<>()、dynamic_cast<<()、const_cast<>()、reinterpret_cast<>(。事实上,C样式转换是超集,编写起来更短。

C风格转换的主要问题是它们隐藏了开发人员转换的真实意图。C样式强制转换几乎可以执行所有类型的强制转换,从static_cast<>()和dynamic_cast<>()执行的通常安全的强制转换到const_cast<()等潜在的危险强制转换,其中可以删除const修饰符,以便可以修改const变量并重新解释cast<>(,甚至可以将整数值重新解释为指针。

这是样品。

int a=rand(); // Random number.

int* pa1=reinterpret_cast<int*>(a); // OK. Here developer clearly expressed he wanted to do this potentially dangerous operation.

int* pa2=static_cast<int*>(a); // Compiler error.
int* pa3=dynamic_cast<int*>(a); // Compiler error.

int* pa4=(int*) a; // OK. C-style cast can do such cast. The question is if it was intentional or developer just did some typo.

*pa4=5; // Program crashes.

C++转换被添加到语言中的主要原因是为了让开发人员明确自己的意图——为什么要进行转换。通过使用在C++中完全有效的C样式转换,您的代码可读性降低,更容易出错,尤其是对于其他没有创建代码的开发人员。因此,为了使代码更加可读和明确,您应该始终倾向于C++转换而不是C样式转换。

这是Bjarne Stroustrup(C++的作者)的书《C++编程语言》第四版第302页的一段简短引用。

这种C样式转换比命名的转换运算符更危险因为在大型程序中很难发现这种符号,而且程序员想要进行的转换也不明确。

让我们在一个示例中看看reinterpret_cast和static_cast的区别:

#include <iostream>
using namespace std;

class A
{
    int a;
};

class B
{
    int b;
};

class C : public A, public B
{
    int c;
};

int main()
{
    {
        B b;
        cout << &b << endl;
        cout << static_cast<C *>(&b) << endl;      // 1
        cout << reinterpret_cast<C *>(&b) << endl; // 2
    }
    cout << endl;
    {
        C c;
        cout << &c << endl;
        cout << static_cast<B *>(&c) << endl;      // 3
        cout << reinterpret_cast<B *>(&c) << endl; // 4
    }
    cout << endl;
    {
        A a;
        cout << &a << endl;
        cout << static_cast<C *>(&a) << endl;
        cout << reinterpret_cast<C *>(&a) << endl;
    }
    cout << endl;
    {
        C c;
        cout << &c << endl;
        cout << static_cast<A *>(&c) << endl;
        cout << reinterpret_cast<A *>(&c) << endl;
    }
    return 0;
}

生成输出:

0x7ffcede34f0c
0x7ffcede34f08 // 1
0x7ffcede34f0c // 2

0x7ffcede34f0c
0x7ffcede34f10 // 3
0x7ffcede34f0c // 4

0x7ffcede34f0c
0x7ffcede34f0c
0x7ffcede34f0c

0x7ffcede34f0c
0x7ffcede34f0c
0x7ffcede34f0c

注意,输出1和2以及3和4是不同的。为什么?在这两种情况下,其中一种是static_cast,另一种是对相同类型的相同输入重新解释cast。

情况可以在下图中看到:

C包含一个B,但B的起始地址与C不同。static_cast正确地计算了C中B的地址。然而,reinterpret_cast返回了我们作为输入给出的相同地址,这在这种情况下是不正确的:该地址没有B。

然而,当在A指针和C指针之间转换时,两个强制转换都返回相同的结果,因为它们恰好在相同的位置开始,顺便说一句,标准无论如何也不能保证这一点。