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 this questions

delete this questions

Scheduled Pinned Locked Moved C / C++ / MFC
question
15 Posts 8 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.
  • S Steve Messer

    It is my understanding the using the following line of code is bad pratice. delete this; Is there ever a good reason to use this construct? Thanks

    M Offline
    M Offline
    Maximilien
    wrote on last edited by
    #5

    with MFC, it's used to delete a modeless dialog.


    Maximilien Lincourt Your Head A Splode - Strong Bad

    1 Reply Last reply
    0
    • S Steve Messer

      Okay so COM makes use of it and also with self deleting classes. I understand the COM aspect but what is a self deleting class? Let me clarify the question a little. Is there a general rule? When would you use delete this? Thanks

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #6

      smesser wrote: what is a self deleting class? A class that deletes itself. Examples are CFrameWnd and CWinThread (when m_bAutoDelete is TRUE).


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


      Honoured as one of The Most Helpful Members of 2004

      Within you lies the power for good; Use it!

      S 1 Reply Last reply
      0
      • PJ ArendsP PJ Arends

        smesser wrote: what is a self deleting class? A class that deletes itself. Examples are CFrameWnd and CWinThread (when m_bAutoDelete is TRUE).


        "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


        Honoured as one of The Most Helpful Members of 2004

        S Offline
        S Offline
        Steve Messer
        wrote on last edited by
        #7

        Yeah, but that is framework stuff. When as a programmer have you used: delete this; Steve

        PJ ArendsP 1 Reply Last reply
        0
        • S Steve Messer

          It is my understanding the using the following line of code is bad pratice. delete this; Is there ever a good reason to use this construct? Thanks

          B Offline
          B Offline
          Brian R
          wrote on last edited by
          #8

          I have been programming with c++ since the Cfront "compiler" came out of ATT. I have never found a circumstance where delete this seemed like a correct solution. My impresssion is that it is an artifact of bad design. The fact that the MFC and COM implementation require it is not a surprise ;-) Along the same line, throwing an exception via a pointer is equally dangerous in my book!

          PJ ArendsP 1 Reply Last reply
          0
          • S Steve Messer

            Yeah, but that is framework stuff. When as a programmer have you used: delete this; Steve

            PJ ArendsP Offline
            PJ ArendsP Offline
            PJ Arends
            wrote on last edited by
            #9

            http://www.codeproject.com/miscctrl/windowscroller.asp[^]


            "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


            Honoured as one of The Most Helpful Members of 2004

            Within you lies the power for good; Use it!

            1 Reply Last reply
            0
            • B Brian R

              I have been programming with c++ since the Cfront "compiler" came out of ATT. I have never found a circumstance where delete this seemed like a correct solution. My impresssion is that it is an artifact of bad design. The fact that the MFC and COM implementation require it is not a surprise ;-) Along the same line, throwing an exception via a pointer is equally dangerous in my book!

              PJ ArendsP Offline
              PJ ArendsP Offline
              PJ Arends
              wrote on last edited by
              #10

              Brian R wrote: it is an artifact of bad design It is a tool that makes it easier to use and then dispose of one time objects without having to keep track of the pointers and memory yourself. Using it is a design decision, not "bad design". Abusing it is bad design, but then abusing anything is.


              "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


              Honoured as one of The Most Helpful Members of 2004

              Within you lies the power for good; Use it!

              M 1 Reply Last reply
              0
              • S Steve Messer

                It is my understanding the using the following line of code is bad pratice. delete this; Is there ever a good reason to use this construct? Thanks

                S Offline
                S Offline
                Steve Messer
                wrote on last edited by
                #11

                thanks all I now have a better understanding of this topic.

                1 Reply Last reply
                0
                • PJ ArendsP PJ Arends

                  Brian R wrote: it is an artifact of bad design It is a tool that makes it easier to use and then dispose of one time objects without having to keep track of the pointers and memory yourself. Using it is a design decision, not "bad design". Abusing it is bad design, but then abusing anything is.


                  "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


                  Honoured as one of The Most Helpful Members of 2004

                  M Offline
                  M Offline
                  Mayur Mahajan
                  wrote on last edited by
                  #12

                  PJ Arends wrote: Using it is a design decision, not "bad design". Abusing it is bad design, but then abusing anything is I am in 100% agreement with you. Even the infamous 'goto' is cursed as a bad design paradigm. But circumstances govern their use in a few specialized cases. After all 'delete this' has certainly come to my rescue when I had to monitor the lifetime of an object using a reference counter similar to the COM classes. I seem a fair use of 'delete this' in such case. Also there are so many things in C++ that if abused screw up the program, better curse programming skill than curse a language ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                  D 1 Reply Last reply
                  0
                  • M Mayur Mahajan

                    PJ Arends wrote: Using it is a design decision, not "bad design". Abusing it is bad design, but then abusing anything is I am in 100% agreement with you. Even the infamous 'goto' is cursed as a bad design paradigm. But circumstances govern their use in a few specialized cases. After all 'delete this' has certainly come to my rescue when I had to monitor the lifetime of an object using a reference counter similar to the COM classes. I seem a fair use of 'delete this' in such case. Also there are so many things in C++ that if abused screw up the program, better curse programming skill than curse a language ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

                    Mayur Mahajan wrote: But circumstances govern their use in a few specialized cases. A paper put together in 1966 by BÖhm, Corrado, and Guiseppe proved theoretically that the GOTO statement was unnecessary; Dijkstra considered it harmful; and Knuth said it might be useful in some circumstances.


                    "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                    M 1 Reply Last reply
                    0
                    • D David Crow

                      Mayur Mahajan wrote: But circumstances govern their use in a few specialized cases. A paper put together in 1966 by BÖhm, Corrado, and Guiseppe proved theoretically that the GOTO statement was unnecessary; Dijkstra considered it harmful; and Knuth said it might be useful in some circumstances.


                      "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                      M Offline
                      M Offline
                      Mayur Mahajan
                      wrote on last edited by
                      #14

                      Well, the main point here is: We have razor sharp tools in the kit. If you can handle them, use it...else they'll hurt you bad ;P ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                      1 Reply Last reply
                      0
                      • S Steve Messer

                        It is my understanding the using the following line of code is bad pratice. delete this; Is there ever a good reason to use this construct? Thanks

                        B Offline
                        B Offline
                        Bob Stanneveld
                        wrote on last edited by
                        #15

                        You can use it in exception classes, which may be created on the stack or dynamically on the heap. When you call some Delete function, you can delete this if the object is created on the heap. I also got the blogging virus..[^]

                        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