C++ inherited delete thingy...

Started by
3 comments, last by Mezz 22 years, 12 months ago
If I point a base class pointer to a derived class allocated with new, when I call delete on the base class pointer will it function correctly. Or will I get some kind of memory leak? -Mezz
Advertisement
Yes it will work as long as you do the right thing which is make the destructor virtual.

I know someone has already answered the question, but I think you will learn more if I explain why you should make the deconstructor virtual.

lets take a look at some code:

#include <iostream>using namespace std;class Base1{public:    Base1() { cout << "Base1::Base1()" << endl; };    ~Base1() { cout << "Base1::~Base1()" << endl; };};class Derived1 : public Base1{public:    Derived1() { cout << "Derived1::Derived1()" << endl; };    ~Derived1() { cout << "Derived1::~Derived1()" << endl; };};class Base2{public:    Base2() { cout << "Base2::Base2()" << endl; };    virtual ~Base2() { cout << "Base2::~Base2()" << endl; };};class Derived2 : public Base2{public:    Derived2() { cout << "Derived2::Derived2()" << endl; };    ~Derived2() { cout << "Derived2::~Derived2()" << endl; };};int main(){    Base1 * b1;    b1 = new Derived1();    delete b1;    Base2 * b2;    b2 = new Derived2();    delete b2;    return 0;}  


When this runs it goves the following output:

Base1::Base1()
Derived1::Derived1()
Base1::~Base1()

Base2::Base2()
Derived2::Derived2()
Derived2::~Derived2()
Base2::~Base2()

What does this mean? When the first example runs it first creates the base class by calling its constructor. Here we created any resources needed by the base class. Next it calls the derrived constructor. And again we create any resourced needed by derived. When the base pointer is deleted it calls the base deconstructor and we can release any resources the base class used. However this leaves the derived classes resources as a "leak".

In the second example the creation happens the same way. however when we get to deleting the pointer to the base things change. Because we have declared the deconstructor virtual it first called the derived deconstructor (allowing us to release any resources it was using) before calling its own deconstructor (again alowing us to clean up its resources).

The point is as Gorg said, if you are writing a class that will be used as a base for inheritance, make the deconstructor virtual and it will "do the right thing".

Alan

EDIT: Grr, stupid brackets.

Edited by - AlanKemp on April 22, 2001 10:50:56 AM
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
follow on:

If I''m not allocating resources, and / or not using constructors / destructors for some reason, will it work then?

-Mezz
You are ALWAYS using a constructor.

If you don't define one, C++ will define a default one. The reason is that it needs to construct the object attributes.


    class A{  /* C++ builds you a default constructor  A(): i(), lst()   {}  */   private:    int i;    std::list lst;}class B:public A{   /*default constructor   B():A(), v()   {}   */      private:  int v;}  C++ will implicity destroy all the attributes and the base class implicity when your object runs out of scope. You don't have to tell him how because a class can only have on destructor. You only to implement a destructor if you allocate pointers or you need to set some attributes somewhere in your program.      void func(){    B b;    /*    compiler will insert a call to b.B()   */   //DO STUFF     /*   before quitting C++ destroy your object  */}    


Anyway, that was a side story.

For you question, if you are not using pointers and are not allocating any memory, then yes it will work if you don't declare a destructor.

But don't be a fool, don't take any chances,just put an empty virtual destructor in your class that you want objects to derive from. Then you are sure you are safe

Edited by - Gorg on April 23, 2001 5:50:03 PM

This topic is closed to new replies.

Advertisement