以下是我的測試代碼。我只是想知道是什麼類型的點,但我在編譯「bool instanceOf(A * msg)」時遇到錯誤。如何糾正它。謝謝。期望的初始值設定項之前'<'令牌
#include <iostream>
using namespace std;
class A
class B : public A
class C : public A
template <class T>
bool instanceOf<T>(A* msg)
{
T *tmp = dynamic_cast<T *>(msg);
if(NULL == tmp)
return false;
return true;
}
void resolveMsgType(A* msg)
{
if(instanceOf<B>(msg))
{
cout<<"this is class B"<<endl;
}else if(instanceOf<C>(msg)){
cout<<"this is class C"<<endl;
}else{
cout<<"error"<<endl;
}
}
int main()
{
A *a;
a = new B();
resolveMsgType(a);
a = new C();
resolveMsgType(a);
return 0;
}
我希望例如'類A'只是一個佔位符而不是真正的代碼? –
在附註中,爲什麼使用: 'if(NULL == tmp) return false; return true;' 當你可以寫 'return tmp!= NULL;' – Dariusz