您的位置:首页 > 编程语言 > C语言/C++

[C/C++]_[0基础]_[static_cast,reinterpret_cast,dynimic_cast的使用场景和差别]

2017-06-17 15:13 309 查看
场景:

1. C++的对象差别于C的原因是他们能够有继承关系, 方法有重载, 覆盖关系等, 他们的对象内存数据结构因此也比較复杂.

2. 非常多情况下我们须要一个父类来存储子类的指针对象进行通用方法的操作。涉及到详细某个子类对象特定操作时又须要强制转换为子类。那么这时候该用什么好呢?

答: 假设不须要类型检查的话就直接用C的强制转换就可以(B*)c. 可是C++ 之父并不推荐使用C的强制类型转换;

原因1是没有编译器检查.

原因2是对象指针在子类父类之间转换时所相应的地址值可能会变化, 这样用C的转换会有误导的可能在那里.

看样例和凝视说明吧:

test.cpp

#include <iostream>

class A
{
public:
A(){}
~A(){}
int i;
int j1;
void Test(){ std::cout << "TestA" << std::endl;}
/* data */
};

class C
{
public:
C(){}
~C(){}
int j;
void Test(){ std::cout << "TestC" << std::endl;}
/* data */
};

class B : public A,public C
{
public:
B(){}
~B(){}
void Test(){ std::cout << "TestB" << std::endl;}
/* data */
};

class K
{

};

// static_cast: 能够进行类型上行和下行转换,会进行类型检查.
// reinterpret_cast: 和C的强转一样,不做类型检查,能够从随意指针类型转换为其它指针类型,非指针类型不能转,比方char to int.
// dynimic_cast:  仅仅适用于多态的类(带virtual)
int main(int argc, char const *argv[])
{
A* a = new A();
B* b = new B();
C* c = b;

std::cout << "(int*)a* :" << (int*)a << " (int)a:" << (int)a << " reinterpret_cast<int>(a):"
<< reinterpret_cast<int>(a) << std::endl;

std::cout << "(int*)b :" << (int*)b << " (int)b:" << (int)b << " reinterpret_cast<int>(b):"
<< reinterpret_cast<int>(b) << std::endl;

// 1.这里假设把c转换为(K*)c,编译不会报错,可是假设使用static_cast<K*>编译会报错.
// 由于static_cast会进行上行的类型检查.
// 注意: 这里(int*)c的地址和上边的(int*)b地址是不一样的,由于他们不是多态关系,并且A,C有成员变量,因此会有偏移量.(没有virtual)
std::cout << "(int*)c :" << (int*)c << " (int)c:" << (int)c << " reinterpret_cast<int>(c):"
<< reinterpret_cast<int>(c) <<  " (B*)c: " << (B*)c << " static_cast<B*>(c):"
<< static_cast<B*>(c) << " static_cast<C*>(b):"
<< static_cast<C*>(b)
<< std::endl;

// 下面编译会报错,dynimc_cast不同意非多态转换,由于没有virtual
// ////error: cannot dynamic_cast 'c' (of type 'class C*') to type 'class B*' (source type is not polymorphic)
// 假设C的构造函数加virtual的话是能够转的,并且带virtual表的地址不会变.
// std::cout << "c* :" << (int*)c << ":" << (int)c << ":"
// 	      << dynamic_cast<B*>(c) << ":"
// 	      << std::endl;
return 0;
}

输出:

(int*)a* :0x2c64e0 (int)a:2909408 reinterpret_cast<int>(a):2909408
(int*)b :0x2c2378 (int)b:2892664 reinterpret_cast<int>(b):2892664
(int*)c :0x2c2380 (int)c:2892672 reinterpret_cast<int>(c):2892672 (B*)c: 0x2c2378 static_cast<B*>(c):0x2c2378 static_cast<C*>(b):0x2c2380

所以你看到下面static_cast的使用方法不要认为奇怪, 它是为了使用编译器检查.

template<typename T> void** IID_PPV_ARGS_Helper(T** pp)
{
static_cast<IUnknown*>(*pp);    // make sure everyone derives from IUnknown
return reinterpret_cast<void**>(pp);
}


參考:

http://www.cnblogs.com/chio/archive/2007/07/18/822389.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐