polymorph help

Started by
10 comments, last by Sneftel 15 years, 10 months ago
ok I want to make a game and in that game I want to have objects or entities whatever you want to call it. now with that said I want to have a base class for all of the objects to inherit and a list of baseclass pointers to all the objects so that way I can loop through the list one time and call generic functions like drawobject or move object and they would function how the specific object would want them to one of the problems I see with this is when it comes down to deleting the objects I have to cast it to whatever object it might be an then delete it is there an easier way or is that the only way to do it if you have a better way of doing this whole thing then please tell me
Advertisement
Moved to the For Beginners forum.
Quote:one of the problems I see with this is when it comes down to deleting the objects I have to cast it to whatever object it might be an then delete it

Why do you need to cast it first?

Steven Yau
[Blog] [Portfolio]

well maybe I dont but how does the compiler know the size of the object its pointing to delete it unless I cast it I just dont want a memory leak
well maybe I dont but how does the compiler know the size of the object its pointing to delete it unless I cast it I just dont want a memory leak
You don't need to cast. The following works perfectly:

class Base {public:    virtual ~Base() {}};class Derived : public base { ... };void func() {    Base *b = new Derived;    delete b;    // Works fine}


See here for why Base has a virtual destructor.

EDIT: Typo.
if this is true then that is so cool thank you
You MUST make sure to make your destructor virtual. That is really all you need
Quote:Original post by helpmenow
well maybe I dont but how does the compiler know the size of the object its pointing to delete it unless I cast it I just dont want a memory leak


The compiler is responsible for tracking how much memory is allocated for objects. The short answer to "how" is "magic". The long answer is that there are a couple of different ways, and you're not supposed to need to know or care how your platform (compiler + compiler options + OS) does it.

You do need to do one thing: make sure a destructor is defined and declared for the base class, and make sure it is 'virtual' (just like all the other functions you want to get polymorphic behaviour from). The destructor is called when an object dies: either when a local variable goes out of scope, or a dynamically allocated instance is deallocated. When you call 'delete' on a pointer, it calls the destructor of the pointed-at thing, per the usual rules of calling a member function through a pointer, and then tells the system to deallocate the memory (and it will figure out how much was allocated). With a virtual destructor, then, the appropriate destructor is called for whatever derived class is actually being pointed at.

So no cast is necessary, exactly the same way that no cast is necessary to use polymorphism for "ordinary" member functions.

Note that you need to do this for the base class even if your destructors don't need to do anything. (But if, for example, none of them do any work, you can just have an declared and defined, virtual, empty destructor in the base, and then not do anything in the derived classes.) This is because the platform is allowed to have that "dynamic dispatch" process involved in the process of figuring out how much memory to deallocate. If the destructor is non-virtual, and you are using delete (as opposed to delete[], which needs to remember the number of elements allocated), the compiler could assume that the size to deallocate is just the size of the pointed-at static type, and that leads to bad things. Further, it's technically undefined behaviour no matter what, so just don't do it, OK? :)
ok so I have to make a virtual destructor but whats going on here is the compiler making all the objects the same size if so then its really no different than an union all though much easier to use than a union but still seems like a waste of memory

This topic is closed to new replies.

Advertisement