33,311
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class A
{
virtual void test() = 0;
};
class B : public A
{
void test()
{
cout << "this is b test()." << endl;
}
};
class C : public A
{
void test()
{
cout << "this is c test()." << endl;
}
};
void func(A* pParam)
{
B* p = NULL;
p = dynamic_cast<B*>(pParam);
if( p == NULL)
{
cout << "not b"<< endl;
}
else
{
cout << "is b"<<endl;
}
}
int main(int argc,char** argv)
{
A* b = new B();
A* c = new C();
func(b);
func(c);
delete b;
b = NULL;
delete c;
c = NULL;
return 0;
}
另外需要注意 rtti 貌似会出现一些移植不兼容的问题。比如苹果的 xcode 似乎就不支持 rtti.
感觉楼主这样的做法其实并不合理,如果想处理不同的参数,完全可以写不同的函数。
更好的办法等楼下解答 。