Error occure when destructor is not virtual ?
-
If i have a base class and a derived class the destrutor of the base class is not virtual & i use a Base ptr to point to Derived Object. An error occures and the base destructor is not called .Why??
class Base { public: Base() { cout << "Constructor: Base" << endl; } ~Base(){ cout << "Destructor : Base" << endl; } }; class temp { public: temp() { cout << "Constructor: temp" << endl; } ~temp(){ cout << "Destructor : temp" << endl; } }; class Derived: public Base ,virtual temp { public: Derived(){ cout << "Constructor: Derived" << endl; } ~Derived(){ cout << "Destructor : Derived" << endl; } }; int main(void) { Base *Var = new Base(); delete Var; return 0; }
Vikas Amin Embin Technology Bombay -
If i have a base class and a derived class the destrutor of the base class is not virtual & i use a Base ptr to point to Derived Object. An error occures and the base destructor is not called .Why??
class Base { public: Base() { cout << "Constructor: Base" << endl; } ~Base(){ cout << "Destructor : Base" << endl; } }; class temp { public: temp() { cout << "Constructor: temp" << endl; } ~temp(){ cout << "Destructor : temp" << endl; } }; class Derived: public Base ,virtual temp { public: Derived(){ cout << "Constructor: Derived" << endl; } ~Derived(){ cout << "Destructor : Derived" << endl; } }; int main(void) { Base *Var = new Base(); delete Var; return 0; }
Vikas Amin Embin Technology BombayWhat error is generated ? Please, when you have errors, crashes and things like try to be as complete as possible. This will help people to understand better your problem.
vikas amin wrote:
i use a Base ptr to point to Derived Object
It is not the case in your sample code. In your code, you just use the base class.
vikas amin wrote:
class Derived: public Base ,virtual temp
Also, what has this
virutal temp
to do there ? If you are using a base pointer that has been allocated as an derived class (Base* ptr = new Derived;
), if you don't specify that your destructor is virtual, then only the destructor of your base class will be called thus this may result in memory leaks (and other problem if you have specific clean-up code in the destructor of your Derived class) -
If i have a base class and a derived class the destrutor of the base class is not virtual & i use a Base ptr to point to Derived Object. An error occures and the base destructor is not called .Why??
class Base { public: Base() { cout << "Constructor: Base" << endl; } ~Base(){ cout << "Destructor : Base" << endl; } }; class temp { public: temp() { cout << "Constructor: temp" << endl; } ~temp(){ cout << "Destructor : temp" << endl; } }; class Derived: public Base ,virtual temp { public: Derived(){ cout << "Constructor: Derived" << endl; } ~Derived(){ cout << "Destructor : Derived" << endl; } }; int main(void) { Base *Var = new Base(); delete Var; return 0; }
Vikas Amin Embin Technology Bombayuse a Base ptr to point to Derived Object
so when deived class is instantiated,first base class constuctor will be called then the derived.llarly when the memory allocated to derived object gets cleaned up the reverse should takes place first the destructor of derived must be called and then base destructor. so if cleaning takes place via base pointer the destructor must be made virtual. but i suggest u to always make base destructor virtual if u want to derive class form the base class never say die
-
What error is generated ? Please, when you have errors, crashes and things like try to be as complete as possible. This will help people to understand better your problem.
vikas amin wrote:
i use a Base ptr to point to Derived Object
It is not the case in your sample code. In your code, you just use the base class.
vikas amin wrote:
class Derived: public Base ,virtual temp
Also, what has this
virutal temp
to do there ? If you are using a base pointer that has been allocated as an derived class (Base* ptr = new Derived;
), if you don't specify that your destructor is virtual, then only the destructor of your base class will be called thus this may result in memory leaks (and other problem if you have specific clean-up code in the destructor of your Derived class)Cedric Moonen: What error is generated ? Please, when you have errors, crashes and things like try to be as complete as possible. This will help people to understand better your problem. Ok will try to take care from next time , post the error also with the code. thanx Vikas Amin Embin Technology Bombay