Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance Confusion

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?

like image 754
Avlagrath Avatar asked Aug 24 '11 05:08

Avlagrath


1 Answers

This is because both the constructor and destructor have run in your code:

object* t = &object();
cout << t->get_type(); 

The &object() creates an (anonymous, temporary) object, immediately destructs it (because it's temporary), and then assigns the address of the now-destructed object to t. (Your compiler may warn you about this expression if you turn the warning level up high enough.)

Try creating an object that sticks around during the time you're using it:

object my_object();
object *t = &my_object;
cout << t->get_type();

When you're using non-virtual functions, the type of the pointer t is used at compile time to determine which method to call. When you're using virtual functions, information inside the object is used at runtime during virtual dispatch to decide which function to call. Since your object is not correctly constructed at the time of the call (since it has already been destructed), the result is strictly undefined but ends up calling the wrong function.

like image 89
Greg Hewgill Avatar answered Nov 16 '22 03:11

Greg Hewgill