Jump to content
  • Advertisement
Sign in to follow this  
helpmenow

polymorph help

This topic is 3660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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? :)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!