• entries
686
1952
• views
387443

# Super n00b code question

102 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.

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].

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

Thanks for the help.

http://www.parashift.com/c++-faq-lite/virtual-functions.html

Inherited base classes that have a virtual destructor imply that your destructor is virtual too, even if you don't declare a destructor or declare one non-virtual.

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. ;)

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.

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.

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.

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. ;)

## Create an account

Register a new account