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. Virtual Destructor

Virtual Destructor

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
3 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.
  • N Offline
    N Offline
    Neelesh K J Jain
    wrote on last edited by
    #1

    What is the main advantage of Virtual destructor? Why don't we have virtual constructor when virtual destructor's are present? Please help regarding this 2 questions. Thanking you, Neelesh K J Jain.

    S C 2 Replies Last reply
    0
    • N Neelesh K J Jain

      What is the main advantage of Virtual destructor? Why don't we have virtual constructor when virtual destructor's are present? Please help regarding this 2 questions. Thanking you, Neelesh K J Jain.

      S Offline
      S Offline
      Serge Krynine
      wrote on last edited by
      #2
      1. See: http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7[^] 2) See http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8[^] Serge
      1 Reply Last reply
      0
      • N Neelesh K J Jain

        What is the main advantage of Virtual destructor? Why don't we have virtual constructor when virtual destructor's are present? Please help regarding this 2 questions. Thanking you, Neelesh K J Jain.

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

        Neelesh K J Jain wrote: What is the main advantage of Virtual destructor? Imagine you have one base class (CBase) and one derived class (CChild that inherits from CBase). Now look at this piece of code: CBase* pPtr = new CChild; .... // Do things with pPtr.. ... delete pPtr; Now, what would happens if the destructor wasn't virtual ? The only destructor that will be called is the destructor of the base class (pPtr is a pointer of type CBase*) and so your object may be not completelly cleaned. So, if you make your destructor virtual, the destructor of CChild will be called. Neelesh K J Jain wrote: Why don't we have virtual constructor when virtual destructor's are present? Because when you create an instance of an object (like in the piece of code before), you know exactly which kind of class (CChild or CBase) you want to create. In fact this line: CBase* pPtr = new CChild can be explained as: first create an object of type CChild (the compiler knows exactly that he will need to call the constructor of CChild) and then store this pointer in pPtr. So, here, no need for virtual constructors... Hope this helps

        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