templates and checking what class it is

Started by
22 comments, last by CpMan 19 years, 8 months ago
Another way wuld be to derive ALL your classes from a single global class, that contains some int giving its type, or a string, or both (which can be useful for debugging)

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
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)
Advertisement
Quote:Original post by caesar4
Another way wuld be to derive ALL your classes from a single global class, that contains some int giving its type, or a string, or both (which can be useful for debugging)


Ewww. [dead]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote:Original post by caesar4
Another way wuld be to derive ALL your classes from a single global class, that contains some int giving its type, or a string, or both (which can be useful for debugging)


Ewww. [dead]


ewww u [imwithstupid]
Cartman's definition of sexual harrasement:"When you are trying to have intercourse with a lady friend, and some other guy comes up and tickles your balls from behind"(watch South Park, it rocks)
I definately wouldn't go for auto. It introduces a lot of potential problems and neccesitates the use of a true runtime. Consider:

auto foo(int x) {
if(x > 0) {
return SomeClass();
}
return x;
}

int main() {
int x = foo(-1);
return 0;
}

The compiler has no way of knowing whether the conversion will succeed at runtime. Making this an error just skirts the general problem. Adding auto would require so many restrictions on it that it isn't worth it anyway.

This is kinda why the ISO committe sucks a bit. It's also why it moves so slow. Too many independent people with independent leanings..so unneccesary things make it into the language..such as....crap...I can't remember the specific annoying example at the moment..I had a big long talk with Herb about it though...I'll get back to you.
VSEDebug Visual Studio.NET Add-In. Enhances debugging in ways never thought possible.

This topic is closed to new replies.

Advertisement