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

delete operator override

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
10 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.
  • A Offline
    A Offline
    Abyss
    wrote on last edited by
    #1

    I'm trying to override the global delete operator.

    void operator delete(void* pAddr)
    {
    ...
    }

    In this method I do some work and then I would like to call the default delete. However

    ::delete(pAddr)

    always call my delete operator. In my code I have to use delete p; which should call my operator but within my operator I would like to call the default system defined delete operator. Any idea how to call (obtain) the default delete operator? Thanks, Abyss

    D B 2 Replies Last reply
    0
    • A Abyss

      I'm trying to override the global delete operator.

      void operator delete(void* pAddr)
      {
      ...
      }

      In this method I do some work and then I would like to call the default delete. However

      ::delete(pAddr)

      always call my delete operator. In my code I have to use delete p; which should call my operator but within my operator I would like to call the default system defined delete operator. Any idea how to call (obtain) the default delete operator? Thanks, Abyss

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

      Abyss wrote:

      In this method...I would like to call the default delete.

      How about calling free(pAddr) instead?

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

      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

      A 1 Reply Last reply
      0
      • D David Crow

        Abyss wrote:

        In this method...I would like to call the default delete.

        How about calling free(pAddr) instead?

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

        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

        A Offline
        A Offline
        Abyss
        wrote on last edited by
        #3

        Not sure if it is OK. My understanding is that a pointer allocated by new operator should be freed by delete and call free on the pointer can cause problems...

        D C 2 Replies Last reply
        0
        • A Abyss

          Not sure if it is OK. My understanding is that a pointer allocated by new operator should be freed by delete and call free on the pointer can cause problems...

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

          Abyss wrote:

          My understanding is that a pointer allocated by new operator should be freed by delete and call free on the pointer can cause problems...

          True, but since you are also overriding the global new operator (hint: you are doing this, aren't you?), that is not an issue.

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

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

          C B 3 Replies Last reply
          0
          • A Abyss

            Not sure if it is OK. My understanding is that a pointer allocated by new operator should be freed by delete and call free on the pointer can cause problems...

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #5

            Probably you can't. Furthermore, if you override delete then you've also to override new, see, for instance [^]. :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            1 Reply Last reply
            0
            • D David Crow

              Abyss wrote:

              My understanding is that a pointer allocated by new operator should be freed by delete and call free on the pointer can cause problems...

              True, but since you are also overriding the global new operator (hint: you are doing this, aren't you?), that is not an issue.

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

              "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              DavidCrow wrote:

              you are doing this, aren't you?

              :laugh:

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              1 Reply Last reply
              0
              • D David Crow

                Abyss wrote:

                My understanding is that a pointer allocated by new operator should be freed by delete and call free on the pointer can cause problems...

                True, but since you are also overriding the global new operator (hint: you are doing this, aren't you?), that is not an issue.

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

                "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                You're too fast... :-D

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                B 1 Reply Last reply
                0
                • A Abyss

                  I'm trying to override the global delete operator.

                  void operator delete(void* pAddr)
                  {
                  ...
                  }

                  In this method I do some work and then I would like to call the default delete. However

                  ::delete(pAddr)

                  always call my delete operator. In my code I have to use delete p; which should call my operator but within my operator I would like to call the default system defined delete operator. Any idea how to call (obtain) the default delete operator? Thanks, Abyss

                  B Offline
                  B Offline
                  Bram van Kampen
                  wrote on last edited by
                  #8

                  Hmm. It strikes me that you have reached a language boundary. Under the cpp model, you can only override functions as follows:; Member Functions in a Derrived Class with the same Param list, Member Functions in your original + Derrived Class class with different Param Lists. Global Functions with different parameter lists. 'delete' is not a global function, but a keyword like 'if' and 'for' you can override it with your own implementation, but, if you do so, you replace it. That is only possible because it is a keyword. If it were a global function, you would not be able to re-define it at all. (you'd get all types of linker errors) My advice is: Re-Design the Code. regards, :)

                  Bram van Kampen

                  1 Reply Last reply
                  0
                  • D David Crow

                    Abyss wrote:

                    My understanding is that a pointer allocated by new operator should be freed by delete and call free on the pointer can cause problems...

                    True, but since you are also overriding the global new operator (hint: you are doing this, aren't you?), that is not an issue.

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

                    "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                    B Offline
                    B Offline
                    Bram van Kampen
                    wrote on last edited by
                    #9

                    delete is a keyword, not a global function!!! :)

                    Bram van Kampen

                    1 Reply Last reply
                    0
                    • C CPallini

                      You're too fast... :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      B Offline
                      B Offline
                      Bram van Kampen
                      wrote on last edited by
                      #10

                      delete is a Keyword, NOT a global function :)

                      Bram van Kampen

                      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