类型转换

  dynamic_cast<>

  C++运行时类型识别RTTI的功能由两个运算符实现:dynamic_cast和typeid,dynamic_cast用于将基类指针或引用转换为派生类指针或引用,一般在以下的情况我们会使用它:想使用基类指针或引用执行派生类的某个操作,但该操作不是虚函数,因为如果该操作是虚函数的话会自动选择执行派生类的虚函数。但我们应该尽量使用虚函数,当无法使用虚函数的时候再使用dynamic_cast。

  当使用dynamic_cast将基类指针或引用转换为派生类指针或引用时,基类必须是多态类型(即包含一个虚函数),且如果要转换的两个对象不是父子关系,dynamic_cast返回NULL,eg:

class CParent
{
public:
    virtual void test() {}
 };

class CChild : public CParent
{    
};

CParent* p = new CChild;
CChild* pt = dynamic_cast<CChild*>(p);
if (pt != nullptr)
{
    //do something
}
else
{
    //error
}

  static_cast<>

  static_cast可以用于将基类指针或引用转换为派生类指针或引用,而且基类不必是多态类型,由于没有动态类型检查,所以是不安全的,如下代码所示,如果使用dynacim_cast<>的话就会避免这种错误,因为dynacim_cast会直接返回NULL,所以如果使用static_cast进行转换的话必须由开发人员保证转换的安全性。

class CParent
{
public:
    virtual void test() {}
 };

class CChild : public CParent
{    
};

CParent* p = new CParent;
CChild* pt = static_cast<CChild*>(p);
if (pt != nullptr)
{
    pt->test(); //error
}

  static_cast也可以用于将派生类指针转换为基类指针(这种转换是安全的)、编译器隐式执行的类型转换(如将int转换为char等,而且这种转换也是由开发人员保证转换安全)、将指定类型转换为void。

  const_cast<>

  const_cast主要针对const和volatile的转换,它可以使一个本来不是const类型的数据转换成const类型或者将const属性去掉,eg:

    string str("test"); 
    char *inbuf = const_cast<char*>(str.c_str());

  reinterpret_cast<>

  reinterpret_cast可以用于进行没有任何关联之间的转换,通过它任何指针都可以转换成其它类型的指针,一般其与强制类型转换功能相同,没有安全检查:

    char* p = new char[4];
    int *pt = reinterpret_cast<int*>(p); //等同于 int *pt = (int*)p;
原文地址:https://www.cnblogs.com/milanleon/p/8807672.html