好像有个函数叫dynamic_cast,但是应该怎么用那?
dynamic_cast<dest_type>(src_obj);
dest_type为目标类型
src_obj为被转换的对象
虚拟处理
楼主啊,给你一个段代码,你运行一下
#include<iostream>
#include<typeinfo>
using namespace std;
class A{};
class B:public A{};
template<typename T1,typename T2>
void getRelation(T2* ptr)
{
dynamic_cast<T1*>(ptr);
cout<<typeid(*ptr).name()<<" is based on class A"<<endl;
}
int main()
{
A* a=new A;
B* b=new B;
getRelation<A>(b);
}
////////////////
不过你不要在你的程序中使用这样的代码,1、无用,2、低效,3、这叫滥用
别说我误导你哟