dynamic_cast
用于具有虚函数的基类与派生类之间的指针或引用的转换。
-
基类必须具备虚函数
原因:dynamic_cast是运行时类型检查,需要运行时类型信息(RTTI),而这个信息是存储与类的虚函数表关系紧密,只有一个类定义了虚函数,才会有虚函数表。
-
运行时检查,转型不成功则返回一个空指针
-
非必要不要使用dynamic_cast,有额外的函数开销
常见的转换方式:
-
基类指针或引用转派生类指针(必须使用dynamic_cast)
-
派生类指针或引用转基类指针(可以使用dynamic_cast,但是更推荐使用static_cast)
#include "stdafx.h"
#include <iostream>
#include <string>
//基类与派生类之间的转换
class CFather
{
public:
CFather() {
m_nTest = 3;
}
virtual void foo() {
std::cout << "CFather()::void foo()" << std::endl;
}
int m_nTest;
};
class CSon : public CFather
{
public:
virtual void foo() {
std::cout << "CSon::void foo()" << std::endl;
}
int m_son;
};
int main() {
CFather f;
CSon s;
CFather* pFather = &f;
CSon* pSon = &s;
//父类转子类(不安全),父类一般空间小,强制转换成子类后,去访问子类的m_son的时候会出现越界,但是只有在运行时才会出现崩溃
// pSon = static_cast<Cson *>(pFather);
// pSon->m_son = 123;
//所以我们希望有一种语法可以检测出这种转换是不安全的,
pSon = dynamic_cast<CSon*>(pFather); //运行时的检测,返回空
if(pSon != nullptr){ //表示转换成功
pSon->m_son = 123;
}
//pSon->foo(); //运行时,pSon为NULL
//pFather = static_cast<CFather* >(&s); //子类转父类,安全
pFather = dynamic_cast<CFather* >(&s); //子类转父类也可以通过dynamic_cast,但不是必须的
pSon = dynamic_cast<CSon*>(pFather); //运行时的检测,可以通过类型检测
pSon->foo();
}