因为,您不愿意使用模板。
您也可以考虑 typeid
使用 c++。例如和更多信息在这里。
还要考虑一个相关的 stackoverflow 问题。
一个可能适合您的工作示例如下:您可以在此处查看输出
#include <typeinfo>
#include <iostream>
#include <string>
using namespace std;
class QueueAbleObject{
};
class A : public QueueAbleObject{
};
class B: public QueueAbleObject{
};
class Queueu{
string mQueueDataType ;
public:
void SetType(const std::type_info& typeInfo){
mQueueDataType = typeInfo.name();
cout << "list type " << mQueueDataType << endl ;
}
bool Insert( QueueAbleObject* obj , const std::type_info& objTypeInfo )
{
if( objTypeInfo.name() != mQueueDataType ){
cout << " Incompatible Object type " <<objTypeInfo.name() << endl;
return false;
}
//do insertionn
return true;
}
};
int main(){
Queueu q;
q.SetType( typeid(A) );
A a;
bool res = q.Insert(&a , typeid(a));
cout << " Insertion of A : " << res << endl;
B b;
res = q.Insert(&b , typeid(b));
cout << " Insertion of B : " << res << endl ;
return 0;
}