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. C++ concept

C++ concept

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
12 Posts 9 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.
  • J Offline
    J Offline
    john5632
    wrote on last edited by
    #1

    Please clear the below confusion; 1. Can you call "delete this;" inside a member function? 2. If Yes, What can you do after calling delete this?

    M H N A J 7 Replies Last reply
    0
    • J john5632

      Please clear the below confusion; 1. Can you call "delete this;" inside a member function? 2. If Yes, What can you do after calling delete this?

      M Offline
      M Offline
      Manfred Rudolf Bihy
      wrote on last edited by
      #2

      It is allowed, but wreaks all kind of havoc depending on what the next statements are in your code. An object instance didn't instantiate itself, so it should neither delete itself. There might still be refernces pointing to said ojbect instance which would become undefined if the object instance would delete itself. Best Regards, -MRB

      1 Reply Last reply
      0
      • J john5632

        Please clear the below confusion; 1. Can you call "delete this;" inside a member function? 2. If Yes, What can you do after calling delete this?

        H Offline
        H Offline
        hmaz4629
        wrote on last edited by
        #3

        u can do this but after this you cant access that mamber function or veriable which is being deleted....!~

        J 1 Reply Last reply
        0
        • J john5632

          Please clear the below confusion; 1. Can you call "delete this;" inside a member function? 2. If Yes, What can you do after calling delete this?

          N Offline
          N Offline
          Niklas L
          wrote on last edited by
          #4

          You can do that, but with care! Afterwards you can not access any instance variables. This implies you can call class methods (declared static) and also other instance methods as long as they don't access any instance variables (in which case they are not truly instance methods anyway.) You can still access class variables (declared static). My recommendation is to not use it unless you really know what you're doing.

          home

          1 Reply Last reply
          0
          • J john5632

            Please clear the below confusion; 1. Can you call "delete this;" inside a member function? 2. If Yes, What can you do after calling delete this?

            A Offline
            A Offline
            Albert Holguin
            wrote on last edited by
            #5

            I think this is one of those things you can do but shouldn't.

            1 Reply Last reply
            0
            • H hmaz4629

              u can do this but after this you cant access that mamber function or veriable which is being deleted....!~

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              hmaz4629 wrote:

              u can do this but after this you cant access that mamber functio

              Technically that depends on the exact nature of the function and maybe the compiler. I say maybe because I would suppose that most C++ compilers are going to generate static bindings for non-virtual methods simply because it produces faster code.

              hmaz4629 wrote:

              or veriable which is being deleted..

              That is certainly unsafe but the actual impact depends again on the compiler and what the variable is and how the application works.

              1 Reply Last reply
              0
              • J john5632

                Please clear the below confusion; 1. Can you call "delete this;" inside a member function? 2. If Yes, What can you do after calling delete this?

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                You can and it might even work. It however is always wrong.

                D N 2 Replies Last reply
                0
                • J jschell

                  You can and it might even work. It however is always wrong.

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

                  jschell wrote:

                  It however is always wrong.

                  Not always.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                  J 1 Reply Last reply
                  0
                  • J jschell

                    You can and it might even work. It however is always wrong.

                    N Offline
                    N Offline
                    Niklas L
                    wrote on last edited by
                    #9

                    This can be used to clean up when implementing COM objects in C++. E.g. when the reference count is zero in Release(). It puts some constraints on instance creation though. You will not get away with automatic objects for instance.

                    home

                    1 Reply Last reply
                    0
                    • J john5632

                      Please clear the below confusion; 1. Can you call "delete this;" inside a member function? 2. If Yes, What can you do after calling delete this?

                      _ Offline
                      _ Offline
                      _Superman_
                      wrote on last edited by
                      #10

                      Like others have said, you cannot access instance member variables after delete this. In addition, I would like to mention that if the object is created on the stack, it will crash. So before you do this you have to ensure that the object is created on the heap. You could probably do this on a singleton implementation.

                      «_Superman_»  _I love work. It gives me something to do between weekends.

                      _Microsoft MVP (Visual C++)

                      Polymorphism in C

                      1 Reply Last reply
                      0
                      • D David Crow

                        jschell wrote:

                        It however is always wrong.

                        Not always.

                        "One man's wage rise is another man's price increase." - Harold Wilson

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        "Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #11

                        Odd. Myself I would consider the design that lead to that requirement wrong.

                        1 Reply Last reply
                        0
                        • J john5632

                          Please clear the below confusion; 1. Can you call "delete this;" inside a member function? 2. If Yes, What can you do after calling delete this?

                          S Offline
                          S Offline
                          Stefan_Lang
                          wrote on last edited by
                          #12

                          You can, but you normally shouldn't. You'll destroy both the variables and the virtual function table (if you have one), so cannot access either. The only things you could still safely do is call static member functions or access static member variables. That said...: - What would happen if you allocated an array of objects of this type? -> crash! - What would happen if you allocated the object on the stack, not on the heap? -> crash! - What would happen if you allocated an object through malloc, a pool allocator, or any other non-standard memory allocator? -> most likely crash - What would happen if you allocated an object of a class derived from this one? -> might work, but could have undesired effects as you're calling the derived class' delete function which may do more than you intended The only way to define such a class in a way that prevents any of the uses above is by protecting access to all the constructors and the destructor, and then provide create() and destroy() functions instead. Or use a friend helper class that does it for you (e. g. smart pointer or factory)

                          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