Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    686
  • comments
    1952
  • views
    387964

Super n00b code question

Sign in to follow this  
Stompy9999

116 views

Okay, I have a noobish programing question that I need answered. I'll use example code, because I don't want to put my real code just to show a simple issue that I need resolved.

Suppose I have class "B", which is derived from class "A":


class A
{
public:
A() {}
virtual void doStuff() {}
};

class B : public A
{
public:
B() {}
~B() {}
void doStuff() {}
};



And if I do this:


A *something = new B();
delete something;



Will the pointer "something" call the deconstructor from class B?

I think it would, but I had my doubts since class A does not have a deconstructor, and it is a pointer to class A, even though it points to a B object.

I'm pretty sure it would call it, but I didn't want to take chances, since I'll be using something like this to free a chunk of data.

Thanks in advance.
Sign in to follow this  


8 Comments


Recommended Comments

Nope, no call to B's deconstructor. Why don't you just put an virtual empty deconstructor in A?

A good way to test things like this is with a Win32 console project and put a simple 'cout<<"B Deconstructor";' or something. (Edit inside of ~B() of course.)

Can't wait for the next demo [grin].

Share this comment


Link to comment
Hmmm, that might be bad.

If I declare a virtual constructor in the base class, would I have to declare one in every derived class?

Thanks for the help.

Share this comment


Link to comment
Whoa... I might be missing something, but why not turn:

A *something = new B();
delete something;

into:

B *something = new B();
delete something;

? I might be overlooking something as usual though. ;)

Share this comment


Link to comment
Thanks MustEatYemen, I'll take a look at that link.

Well, it actually has to do with how I store enemies. All enemies are represented during the game as a pointer to the base enemy class, but they store objects of derived classes.

Share this comment


Link to comment
Quote:
Whoa... I might be missing something, but why not turn:

A *something = new B();
delete something;

into:

B *something = new B();
delete something;

? I might be overlooking something as usual though. ;)


Well for managing a collection of objects, representing say media (art resrouces, polygon data, and sound) or game objects (sprites, weapons, etc) then you may want to have a uniform method of storing and calling those objects. Because C++ lacks in the inspection and the reflection department, usually pointers are passed around as a base Object * class, and the actual function is called via the v-table.

This way, you can store all objects with like interfaces, but not like implementations in one system, instead of having multiple data structures floating around doing the same thing.

Share this comment


Link to comment
As noted, no, it won't call your derived class's constructor. Every class has a destructor; if you don't write it yourself, the compiler automatically generates it, and the auto-generated version is nonvirtual. So, the nonvirtual base class destructor gets called, and doesn't forward the call downwards to the base class.

As noted, the way to fix this is to put an empty virtual destructor in your base class.

Share this comment


Link to comment
Quote:

This way, you can store all objects with like interfaces, but not like implementations in one system, instead of having multiple data structures floating around doing the same thing.


Ah, ok, now I understand why the prior approach might be used. Thanks for the explanation. ;)

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!