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. operator delete

operator delete

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
6 Posts 4 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.
  • E Offline
    E Offline
    Eikthrynir
    wrote on last edited by
    #1

    Hello! If I define a type, MyClass, for example, and do the following: MyClass *pObj = new MyClass; void *pv = pObj; I know that I must NOT do something like this: delete pv; because the destructor pObj->MyClass::~MyClass() will NOT be called. But why does the destructor get called when I delete pObj; because the operator delete function gets a void * as parameter. Isn't it the same problem? I mean, how does the operator delete function "know" that pObj converted to void * is actually a MyClass object? Thanks in advance!

    D C 2 Replies Last reply
    0
    • E Eikthrynir

      Hello! If I define a type, MyClass, for example, and do the following: MyClass *pObj = new MyClass; void *pv = pObj; I know that I must NOT do something like this: delete pv; because the destructor pObj->MyClass::~MyClass() will NOT be called. But why does the destructor get called when I delete pObj; because the operator delete function gets a void * as parameter. Isn't it the same problem? I mean, how does the operator delete function "know" that pObj converted to void * is actually a MyClass object? Thanks in advance!

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Eikthrynir wrote:

      But why does the destructor get called when I delete pObj;

      Because that's what happens when you use delete on an object.

      Eikthrynir wrote:

      because the operator delete function gets a void * as parameter.

      Destructors don't take arguments.

      "Love people and use things, not love things and use people." - Unknown

      "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

      L 1 Reply Last reply
      0
      • E Eikthrynir

        Hello! If I define a type, MyClass, for example, and do the following: MyClass *pObj = new MyClass; void *pv = pObj; I know that I must NOT do something like this: delete pv; because the destructor pObj->MyClass::~MyClass() will NOT be called. But why does the destructor get called when I delete pObj; because the operator delete function gets a void * as parameter. Isn't it the same problem? I mean, how does the operator delete function "know" that pObj converted to void * is actually a MyClass object? Thanks in advance!

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

        Eikthrynir wrote:

        But why does the destructor get called when I delete pObj;

        The compiler does the job: it sees that you are calling delete on a MyClass object, so it supposes that it is a MyClass object and calls its destructor. If you delete a void pointer, the compiler only sees a void pointer.

        Cédric Moonen Software developer
        Charting control [v1.4]

        L 1 Reply Last reply
        0
        • D David Crow

          Eikthrynir wrote:

          But why does the destructor get called when I delete pObj;

          Because that's what happens when you use delete on an object.

          Eikthrynir wrote:

          because the operator delete function gets a void * as parameter.

          Destructors don't take arguments.

          "Love people and use things, not love things and use people." - Unknown

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

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

          Wow, I can barely read his post but yours is rated a '1'? Well, I'll just have to do something about that. :-\

          led mike

          D 1 Reply Last reply
          0
          • L led mike

            Wow, I can barely read his post but yours is rated a '1'? Well, I'll just have to do something about that. :-\

            led mike

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            We're number one! We're number one!

            "Love people and use things, not love things and use people." - Unknown

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            1 Reply Last reply
            0
            • C Cedric Moonen

              Eikthrynir wrote:

              But why does the destructor get called when I delete pObj;

              The compiler does the job: it sees that you are calling delete on a MyClass object, so it supposes that it is a MyClass object and calls its destructor. If you delete a void pointer, the compiler only sees a void pointer.

              Cédric Moonen Software developer
              Charting control [v1.4]

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

              Cedric Moonen wrote:

              so it supposes that it is a MyClass object

              I reckon so. :rolleyes:

              led mike

              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