What is the advantage to declare destructor as a virtual in c++ ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • todashah
    New Member
    • Feb 2008
    • 26

    What is the advantage to declare destructor as a virtual in c++ ?

    I have idea about virtual function in c++. It is supported dynamic binding but i have no idea about what is the advantage to declare any destructor as a virtual in c++ ? Explain with suitable example....

    Bye...Take Care...Give Reply
  • scruggsy
    New Member
    • Mar 2007
    • 147

    #2
    Originally posted by todashah
    I have idea about virtual function in c++. It is supported dynamic binding but i have no idea about what is the advantage to declare any destructor as a virtual in c++ ? Explain with suitable example....

    Bye...Take Care...Give Reply
    Here.
    Destructors are also sometimes made virtual simply to satisfy the requirement that a polymorphic class must have at least one virtual function.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      If you delete a class through a base class pointer then if the destructor of the base class is not virtual the destructor of the actual class is not called, the same as for any other function called though a base class pointer.

      Removing a class without properly calling its destructor could have serious implications for program stability.

      Comment

      • SpecialKay
        New Member
        • Mar 2008
        • 109

        #4
        you can avoid memorie leaks.

        Comment

        • mvjohn100
          New Member
          • Mar 2008
          • 57

          #5
          For work with "dynamic_ca st" the class should have a virtual table. so to have a virtual table at least one member function should be virtual. making destructor as virtual satisfy the demand.

          Comment

          • haneeshkb
            New Member
            • Nov 2007
            • 23

            #6
            Originally posted by todashah
            I have idea about virtual function in c++. It is supported dynamic binding but i have no idea about what is the advantage to declare any destructor as a virtual in c++ ? Explain with suitable example....

            Bye...Take Care...Give Reply

            virtual destructer is used so that while deleting the pointer to base class but pointing to base class invokes the derived class destructor first,then the base class destruct hence preventing memory leak...

            First pls get clear with virtual function concepts...
            suppose we have a class hierachy
            Empl-->Manager-->CEO
            now we are creating
            Empl* e1;
            CEO c1;
            e1= & c1;
            Now when ever my e1 goes out of scope because e1 is of type Empl ,only the destructor of e1 is called & memory is freed for attributes of e1 only. so we have the memory leakage problem
            so if we are making Destructor as a virtual so exceutin of destructor will follow
            ~CEO-->~Manager-->~Empl path so Memory for all the data is freed.....
            so to avoid these Memory Leakage problem we have to make destructor as a virtual destructor.....

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              I just published this article in the C/C++ HowTos. Your answer is there.

              http://bytes.com/forum/thread793836.html.

              Comment

              Working...