Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Error occure when destructor is not virtual ?

Error occure when destructor is not virtual ?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vikas amin
    wrote on last edited by
    #1

    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

    C S 2 Replies Last reply
    0
    • V vikas amin

      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

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      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)

      V 1 Reply Last reply
      0
      • V vikas amin

        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

        S Offline
        S Offline
        sunit5
        wrote on last edited by
        #3

        use 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

        1 Reply Last reply
        0
        • C 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.

          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)

          V Offline
          V Offline
          vikas amin
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups