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. destructor on pointer of parent class

destructor on pointer of parent class

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
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.
  • A Offline
    A Offline
    Anand Vivek Srivastava
    wrote on last edited by
    #1

    is this wrong? if yes, how bad is it? class B extends class A. each has a virtual destructor.

    A* a = new B();
    delete a;

    by how bad I mean is the loss only in form of memory leak or could this lead to memory corruption?

    L J 2 Replies Last reply
    0
    • A Anand Vivek Srivastava

      is this wrong? if yes, how bad is it? class B extends class A. each has a virtual destructor.

      A* a = new B();
      delete a;

      by how bad I mean is the loss only in form of memory leak or could this lead to memory corruption?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      avsrivastava wrote:

      each has a virtual destructor.

      Then their is no problem ~B will be called first and then ~A.

      "Alot of the people on this forum are incredibly stupid, thinking that the internet is real"
      Score: 1.0 in the Soap Box

      led mike

      1 Reply Last reply
      0
      • A Anand Vivek Srivastava

        is this wrong? if yes, how bad is it? class B extends class A. each has a virtual destructor.

        A* a = new B();
        delete a;

        by how bad I mean is the loss only in form of memory leak or could this lead to memory corruption?

        J Offline
        J Offline
        Jun Du
        wrote on last edited by
        #3

        No, it's fine. In this case, B's destructor doesn't have to be virtual; only the base class needs to be.

        Best, Jun

        A 1 Reply Last reply
        0
        • J Jun Du

          No, it's fine. In this case, B's destructor doesn't have to be virtual; only the base class needs to be.

          Best, Jun

          A Offline
          A Offline
          Anand Vivek Srivastava
          wrote on last edited by
          #4

          thanks both of you, got the point. makes me wonder how people would have managed to code if there was no dynamic dispatch. :)

          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