Alright so I've been working on a game engine, and I'm running into a problem... Currently I have a hierarchy of classes (image->animation->object) all of which have a method get_type() and I'm having this problem:
if I declare a derived class using the new keyword, or statically, I get the desired results:
object instance; cout << instance.get_type(); //returns an int value object* pointer = new(object); cout << pointer->get_type(); From the above code, the console outputs 3. The method is declared as such in object:
Image Class:
class image{ public: virtual int get_type(); }; int object::get_type(){ return 1; } Animation Class:
class animation: public image{ public: virtual int get_type(); }; int animation::get_type(){ return 2; } Object Class:
class object: public animation{ public: virtual int get_type(); }; int object::get_type(){ return 3; } Now the problem arises when I do something like this:
object* t = &object(); cout << t->get_type(); The result is now 1.
If I remove the virtual keyword from the class declaration of the object class, it works as I expect (the last bit of code returns 3)
Question: how can I utilize my virtual methods from the object* pointer without using the new keyword? Reason being, I'm filling a vector with pointers to these objects and creating them as I'm adding them, so I hope to be able to do something like the following:
vector<object*> objects; //not imgs or animations for (int a = 0; a < 20; a++){ objects.push_back(&object()); } And have any object[x]->get_type(); return the correct class type. The reason for this is an event system that I'm using in my game that needs the origination class type...
TLDR; I really stink at inheritance, can you help?