【C++】static_cast,const_cast,dynamic_cast,reinterpert_cast

本文介绍C++中四个类型转换符:static_cast、dynamic_cast、const_cast和reinterpret_cast。

1. static_cast

static_cast应用于以下三种场景:
(1)基本数据类型之间的转换

int main()
{
    float pi = 3.1415;
    int a = static_cast<int>(pi);

    cout << pi << " " << a << endl;
}

(2) void*与目标类型的指针间的转换

int main()
{
    int n = 10;
    void *pv = &n;
    int *p = static_cast<int*>(pv);

    cout << n << " " << *p;
}

int main()
{
    int n = 10;
    int *pn = &n;
    void *pv = static_cast<void*>(pn);
    int *p = static_cast<int*>(pv);

    cout << n << " " << *p;
}

【注】static_cast不能转换掉变量的const、volatile和__unaligned属性

(3)基类与派生类指针或引用类型之间的转换
此时不做运行时检测,不如dynamic_cast安全。

  • 当进行上行转换,也就是把子类的指针或引用转换成父类表示,这种转换是安全的;
  • 当进行下行转换,也就是把父类的指针或引用转换成子类表示,这种转换是不安全的;
class Base {
public:
    Base() { cout << "Base Constructor\n"; }
    virtual ~Base() { cout << "Base Destructor\n"; }
    virtual void print() {
        cout << "print in Base\n";
    }
};

class Derived : public Base {
public:
    Derived() { cout << "Derived Constructor\n"; }
    virtual ~Derived() { cout << "Derived Destructor\n"; }

    virtual void print() {
        cout << "print in Derived\n";
    }

    virtual void func() {
        cout << "csdn blog\n";
    }

private:
    int x;
};

int main()
{
    // 基类指针转为派生类指针,且该基类指针指向基类对象
    Base *pb1 = new Base;
    Derived *pd1 = static_cast<Derived*>(pb1);
    // pd1->func(); // run time error

    // 基类指针转为派生类指针,且该基类指针指向派生类对象
    Base *pb2 = new Derived;
    Derived *pd2 = static_cast<Derived*>(pb2);
    pd2->func(); // right

    // 派生类指针转为基类指针
    Derived *pd3 = new Derived;
    Base *pb3 = static_cast<Base*>(pd3);
    pb3->print(); // right
}

2. const_cast

const_cast用来将变量的const、volatile和__unaligned属性移除。常量指针被转换成非常量指针,并且仍然指向原来的对象;常量引用被转换成非常量引用,并且仍然引用原来的对象

class CSDN
{
public:
    CSDN() : v(10){};

    int v;
};

int main()
{
    const CSDN *pa = new CSDN;
    // pa->v = 100; // error
    CSDN *pb = const_cast<CSDN*>(pa);
    pb->v = 100;

    // pa、pb指向同一个对象
    cout << pa->v << "\n" << pb->v << endl;

    const CSDN &refa = *pa;
    // refa.v = 200; // error
    CSDN &refb = const_cast<CSDN&>(refa);
    refb.v = 200;

    // refa、refb引用同一个对象
    cout << refa.v << "\n" << refb.v << endl;
}

3. reinterpret_cast

处理无关类型之间的转换;它会产生一个新的值,这个值会有与原始参数(expressoin)有完全相同的比特位。它可以将一个指针转换成一个整数,也可以将一个整数转换成一个指针,在实际开发中,先把一个指针转换成一个整数,再把该整数转换成原类型的指针,还可以得到原来的指针值;特别是开辟了系统全局的内存空间,需要在多个应用程序之间使用时,需要彼此共享,传递这个内存空间的指针时,就可以将指针转换成整数值,得到以后,再将整数值转换成指针,进行对应的操作。

void printInt(void* a) {
    int n = reinterpret_cast<int>(a);

    cout << n << endl;
}

int main()
{
    int n = 10;
    void *a = reinterpret_cast<void*>(n);
    printInt(a);
}

4. dynamic_cast

参考

[1] http://www.jb51.net/article/55885.htm
[2] http://www.cnblogs.com/QG-whz/p/4517336.html
[3] http://blog.csdn.net/wenpinglaoyao/article/details/50497169

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章