Is there a way to tell what type of class an object is?
I want to tag objects of a few types with an id integer, and I want the classes to have seperate id index counters. The problem I'm running into is with the base class, as it will increment the id index even though the object is not of the appropriate class.
As you can see, object1_count will be incremented even when it's not used. I want object1_count to be incremented only when the object is the base class, not the derived class.Code:#define OBJECT1 0 #define OBJECT2 1 #define OBJECT3 2 unsigned short int object1_count = 0, object2_count = 0, object3_count = 0; class object1 { public: unsigned short int type, id; object1(void) { type = OBJECT1; id = object1_count++; } }; class object2: public object1 { public: object2(void) { type = OBJECT2; id = object2_count++; } }; class object3: public object1 { public: object3(void) { type = OBJECT3; id = object3_count++; } };
I know I could use the constructor overloading thing I learned in my last thread to accomplish this, but since it's a test project and the whole purpose is to learn new things, I thought I'd look into this to see if I could do it another way.
If I have a way to say "if this object is of class object1", such as...
Obviously, I don't think I can do it like that, but it was just an example to show what type of ability I'm after.Code:class object1 { public: object1(void) { if(this->class == object1){id = object1_count++;} } };