class Base {public:
Base() = default;
~Base() = default;
virtual void Hello() {std::cout<< "Hello"<< std::endl;
}
void Func() {std::cout<< "Func with Base"<< std::endl;
}
};
class DervideA:public Base {public:
DervideA() = default;
~DervideA() = default;
void Hello() override {std::cout<< "Hello A"<< std::endl;
}
void doSomething() {std::cout<< "do something"<< std::endl;
return;
}
void Func() {std::cout<< "Func with DervideA"<< std::endl;
}
};
class DervideB:public Base {public:
DervideB() = default;
~DervideB() = default;
void Hello() override{std::cout<< "Hello B"<< std::endl;
}
void Func() {std::cout<< "Func with DervideB"<< std::endl;
}
};
3.2 通過(guò)派生類指針調(diào)用基類的方法int main() {DervideA *pDervideA = new (std::nothrow) DervideA;
if (pDervideA) {pDervideA->Func();
//場(chǎng)景一:派生類指針轉(zhuǎn)換為基類指針,使用基類的方法
if (Base *pBase = dynamic_cast (pDervideA)) {pBase->Func();
}
}
delete pDervideA;
return 0;
}
[root@VM-8-15-centos c++]# ./../../../bin/RTTI_test_demo
Func with DervideA
Func with Base
3.3 通過(guò)指向派生類的基類指針調(diào)用派生類A有而派生類B沒有的方法void Hello(Base *pBase) {//基類的虛函數(shù),編譯器保證的安全的保證
pBase->Hello();
//場(chǎng)景二:基類指針轉(zhuǎn)換指定的派生類指針
//doSomething方法僅在DervideA中有,無(wú)法直接通過(guò)pBase使用??梢詫⒒愔羔樛ㄟ^(guò)dynamic_cast轉(zhuǎn)換成DervideA的指針進(jìn)行使用
//如果pBase是指向DervideB的,則轉(zhuǎn)換失敗
DervideA *pDervideA = dynamic_cast(pBase);
if (pDervideA)
pDervideA->doSomething();
else
std::cout<< "dynamic_cast failed!"<< std::endl;
}
int main() {Base *p = new (std::nothrow) DervideA;
if (p)
::Hello(p);
delete p;
p = new (std::nothrow) DervideB;
if (p)
::Hello(p);
delete p;
return 0;
}
[root@VM-8-15-centos c++]# ./../../../bin/RTTI_test_demo
Hello A
do something
Hello B
dynamic_cast failed!
3.4 引用版本void Hello(Base& refBase)
{refBase.Hello();
//場(chǎng)景二:基類引用轉(zhuǎn)換指定的派生類引用
//doSomething方法僅在DervideA中有,無(wú)法直接使用??梢詫⒒愐猛ㄟ^(guò)dynamic_cast轉(zhuǎn)換成DervideA的引用進(jìn)行使用
//如果轉(zhuǎn)換失敗,拋出異常std::bad_cast
try
{DervideA& refDervideA = dynamic_cast(refBase);
refDervideA.doSomething();
}
catch(const std::exception& e)
{std::cout<< "dynamic_cast failed!"<< std::endl;
std::cerr<< e.what()<< '\n';
}
}
int main()
{Base *p = new (std::nothrow) DervideA;
if (p)
::Hello(*p);
delete p;
p = new (std::nothrow) DervideB;
if (p)
::Hello(*p);
delete p;
DervideA *pDervideA = new (std::nothrow) DervideA;
if (pDervideA) {pDervideA->Func();
//場(chǎng)景一:派生類引用轉(zhuǎn)換為基類引用,使用基類的方法
try
{Base& refBase = dynamic_cast (*pDervideA);
refBase.Func();
}
catch(const std::exception& e)
{std::cout<< "dynamic_cast failed!"<< std::endl;
std::cerr<< e.what()<< '\n';
}
}
}
運(yùn)行結(jié)果:
[root@VM-8-15-centos c++]# ./../../../bin/RTTI_test_demo
Hello A
do something
Hello B
dynamic_cast failed!
std::bad_cast
Func with DervideA
Func with Base
二. typeid
1.用途void typeid_test(Base *pBase)
{//typeid對(duì)象指針無(wú)法獲取指針?biāo)笇?duì)象的類類型
if (typeid(pBase) == typeid(Base))
std::cout<< "Base"<< std::endl;
//typeid對(duì)象或其引用得出的結(jié)果才是對(duì)象的類型
if (typeid(*pBase) == typeid(DervideA)) {std::cout<< "DervideA"<< std::endl;
}
else if (typeid(*pBase) == typeid(DervideB)) {std::cout<< "DervideB"<< std::endl;
}
}
int main()
{int array[] = {1,2,3};
//typeid一個(gè)數(shù)組的結(jié)果不是數(shù)組類型的指針
if (typeid(array) == typeid(int*))
std::cout<< "type of array is int-pointer!"<< std::endl;
else
std::cout<< "type of array is not int-pointer!"<< std::endl;
Base *p = new (std::nothrow) DervideA;
if (p)
:: typeid_test(p);
delete p;
p = new (std::nothrow) DervideB;
if (p)
::typeid_test(p);
delete p;
return 0;
}
[root@VM-8-15-centos c++]# ./../../../bin/RTTI_test_demo
type of array is not int-pointer!
DervideA
DervideB
四.參考文獻(xiàn)C++ Primer 第五版
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧