一、dynamic_cast运算符介绍
(1)运行阶段类型识别(RTTI, Runtime Type Identification)为程序在阶段确定对象的类型,只适用于包含虚函数的类。
(2)基类指针可以指向派生类对象,想要调用派生类对象的非虚函数需要使用dynamic_cast运算符,dynamic_cast运算符用指向基类的指针来生成派生类指针;
(3)语法:
派生类指针 = dynamic_cast<派生类类型*>(基类指针);
(4)如果转换不成功,dynamic_cast返回nullptr。
注意:
(1)dynamic_cast可以将派生类指针转化为基类指针,这种做法没有任何意义;
(2)dynamic_cast可以用于引用,但是没有空指针对应引用值,如果转换请求不正确,会出现bad_cast异常;
(3)dynamic_cast只适用于包含虚函数的类。
二、例子
1、多态的例子
以下是一个正常的多态案例:
#include <iostream>
using namespace std;class Hero
{
public:Hero() {cout << "基类默认构造函数Hero()" << endl;}virtual ~Hero() {cout << "基类析构函数~Hero" << endl;}
public:virtual void skill1() { cout << "英雄释放1技能" << endl; }virtual void skill2() { cout << "英雄释放2技能" << endl; }virtual void skill3() { cout << "英雄释放大招" << endl; }
};class LLW: public Hero
{
public:LLW() {cout << "派生类默认构造函数LLW()" << endl;}~LLW(){cout << "派生类析构函数~LLW()" << endl;}
public:void skill1() { cout << "兰陵王释放1技能" << endl; }void skill2() { cout << "兰陵王释放2技能" << endl; }void skill3() { cout << "兰陵王释放大招" << endl; }
};class DW : public Hero
{
public:DW() {cout << "派生类默认构造函数DW()" << endl;}~DW() {cout << "派生类析构函数~DW()" << endl;}
public:void skill1() { cout << "典韦释放1技能" << endl; }void skill2() { cout << "典韦释放2技能" << endl; }void skill3() { cout << "典韦释放大招" << endl; }
};class HY : public Hero
{
public:HY() {cout << "派生类默认构造函数HY()" << endl;}~HY() {cout << "派生类析构函数~HY()" << endl;}
public:void skill1() { cout << "后裔释放1技能" << endl; }void skill2() { cout << "后裔释放2技能" << endl; }void skill3() { cout << "后裔释放大招" << endl; }
};int main()
{int id;cout << "请输入英雄(1-兰陵王 2-典韦 3-后裔):";cin >> id;Hero* hero = nullptr;switch (id){case 1:hero = new LLW; break;case 2:hero = new DW; break;case 3:hero = new HY; break;default:break;}if (hero != nullptr) {hero->skill1();hero->skill2();hero->skill3();}delete hero;hero = nullptr;
}
2、强转基类指针
如果想要调用派生类的非重写函数,必须转化基类指针为派生类指针:
#include <iostream>
using namespace std;class Hero
{
public:Hero() {cout << "基类默认构造函数Hero()" << endl;}virtual ~Hero() {cout << "基类析构函数~Hero" << endl;}
public:virtual void skill1() { cout << "英雄释放1技能" << endl; }virtual void skill2() { cout << "英雄释放2技能" << endl; }virtual void skill3() { cout << "英雄释放大招" << endl; }
};class LLW : public Hero
{
public:LLW() {cout << "派生类默认构造函数LLW()" << endl;}~LLW() {cout << "派生类析构函数~LLW()" << endl;}
public:void skill1() { cout << "兰陵王释放1技能" << endl; }void skill2() { cout << "兰陵王释放2技能" << endl; }void skill3() { cout << "兰陵王释放大招" << endl; }
};class DW : public Hero
{
public:DW() {cout << "派生类默认构造函数DW()" << endl;}~DW() {cout << "派生类析构函数~DW()" << endl;}
public:void skill1() { cout << "典韦释放1技能" << endl; }void skill2() { cout << "典韦释放2技能" << endl; }void skill3() { cout << "典韦释放大招" << endl; }public:void show() {cout << "典韦SHOW." << endl;}
};class HY : public Hero
{
public:HY() {cout << "派生类默认构造函数HY()" << endl;}~HY() {cout << "派生类析构函数~HY()" << endl;}
public:void skill1() { cout << "后裔释放1技能" << endl; }void skill2() { cout << "后裔释放2技能" << endl; }void skill3() { cout << "后裔释放大招" << endl; }
};int main()
{int id;cout << "请输入英雄(1-兰陵王 2-典韦 3-后裔):";cin >> id;Hero* hero = nullptr;switch (id){case 1:hero = new LLW; break;case 2:hero = new DW; break;case 3:hero = new HY; break;default:break;}if (hero != nullptr) {hero->skill1();hero->skill2();hero->skill3();if (id == 2) {DW* dw = (DW*)hero;dw->show();}}delete hero;hero = nullptr;
}
以下代码,当为典韦时,新建一个典韦指针,指向强转类型才能调用DW类的非重写函数
if (id == 2) {DW* dw = (DW*)hero;dw->show();
}
3、dynamic_cast操作符的使用
DW* dw = dynamic_cast<DW*>(hero);
if (dw != nullptr) {dw->show();
}
当指针不为nullptr的时候,则代表转换成功。