//模板类类myFooBase { fooBase * m_pPtr; }; 模板< class T> class myFoo:public myFooBase {公开: myFoo(fooBase * p_pPtr): m_pPtr(p_pPtr) {} bool isCompatible(myFoo * p_p) // //检查,如果p_p兼容(dynamic_castable)到T //可能的方法: T * TempPtr = new T(); if(dynamic_cast< T *>(p_p-> m_pPtr))} }; // Template class class myFooBase { fooBase *m_pPtr; }; template <class T> class myFoo : public myFooBase { public: myFoo(fooBase *p_pPtr) : m_pPtr(p_pPtr) { } bool isCompatible(myFoo *p_p) { // Check, if p_p is compatible (dynamic_castable) to T // Possible way: T *TempPtr = new T(); if (dynamic_cast<T *> (p_p->m_pPtr)) } }; 为什么要泄漏内存?为何选择新T()? " if dynamic_cast<>()"一个人会做的很好。顺便说一句:如果你是,只需写下myFoo * p_p即可。在参数列表中它实际上是 表示myFoo< T> * P_P" - 即如果你只将Ts(或派生的) 指定给m_pPtr它总是兼容的! 我认为你要做的事情会是这样的: //在模板内< class T> class myFoo ...: template< class U> bool isCompatible(myFoo< U> const& other) { 返回!! dynamic_cast< T *>(other.m_pPtr); } Why do you want to leak memory? Why "new T()"? The"if dynamic_cast<>()" alone will do just fine. BTW: if youjust write "myFoo *p_p" in the parameter list it actuallymeans "myFoo<T> *p_p" - i.e. if you only assign Ts (or derived)to "m_pPtr" it will always be compatible!I think what you''re trying to do would read something like: // inside template <class T> class myFoo ...:template<class U>bool isCompatible( myFoo<U> const & other ){return !! dynamic_cast<T*>(other.m_pPtr);} 如何使用typeid检查A类是否相同或b b的派生? 我已经解释了我的问题错了...我很抱歉我的英语不好! 我有一个更好的例子: 模板< A类,B类> bool testDerivation() { //检查,如果A是相同的或B类的推导 // typeid(A)== typeid( B)可能......但仅限于 平等......衍生怎么样? } 如何,实现这样的功能? 正如我所说,我不能使用dynamic_cast,因为B(或A)可以是抽象的。 因此我无法创建实例。 How to use typeid to check if a class A is the same or a derivation ofclass B? I have explained my problem wrong... I''m sorry for my bad English! I have a better example: template <class A, class B>bool testDerivation(){// check, if A is the same or a derivation of class B // typeid(A) == typeid(B) would be possible.... but only forEquality... what about derivation?} How, to implement such a function?As I said, I cannot use dynamic_cast, because B (or A) can be abstract.Because of this I cannot create an instance. 这篇关于" dynamic_cast的"与抽象类的实例?!?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 20:26