typedef enum _EClassType{ ectBaseOfAll = 1, ...... // enumerate your classes ectA, ectB,} EClassType;class CBaseOfAll{ public: CBaseOfAll(){ ectClassType = ectBaseOfAll; csClassName = "CBaseOfAll"; } bool IsSameType(CBaseOfAll *ptr){ return (ectClassType == ptr->ectClassType); } EClassType ectClassType; CString csClassName;};class A : public CBaseOfAll{ public: A(){ ectClassType = ectA; csClassName = "A"; //// continue with a custom destructor }};class B : public CBaseOfAll{ public: B(){ ectClassType = ectB; csClassName = "B"; //// continue with a custom destructor }};A *class1 = new A();B *class2 = new B();A *class3 = new A();if(class1->IsSameType(class2)) print("class2 is same"); else print("class2 is diff"); // this will be printedif(class1->IsSameType(class3)) print("class3 is same"); // this will be printedelse print("class3 is diff");
Thats my method, codewise, its slightly more work than using typeid(), but in the end proves to be faster
there is another plus in doing it the way i have
with the csClassName, u can implement a class factory, and when passing derived classes cast as the base type, functions can be aware of what they're getting