this Pointer

This is a discussion on this Pointer within the C++ Programming forums, part of the General Programming Boards category; referring to http://www.parashift.com/c++-faq-lit...html#faq-16.15 not by new[], nor by placement new, nor a local object on the stack, nor a global, ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    this Pointer

    referring to http://www.parashift.com/c++-faq-lit...html#faq-16.15

    not by new[], nor by placement new, nor a local object on the stack, nor a global, nor a member of another object; but by plain ordinary new - where are global objects stored?

    You must be absolutely 100% positive sure that your member function will be the last member function invoked on this object. - how is this possible when delete this itself will call the destructor part of object, so the func. calling delete this will be last?


    Naturally the usual caveats apply in cases where your this pointer is a pointer to a base class when you don't have a virtual destructor. - how can a this pointer point to base class ? and if such is a case, is this too polymorphic?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by vb.bajpai View Post
    not by new[], nor by placement new, nor a local object on the stack, nor a global, nor a member of another object; but by plain ordinary new - where are global objects stored?
    This is one of those platform specific items, I believe. If you didn't not create an object via new and it's global, then you cannot delete it. It's stored somewhere special. Just trust that.

    Quote Originally Posted by vb.bajpai View Post
    You must be absolutely 100% positive sure that your member function will be the last member function invoked on this object. - how is this possible when delete this itself will call the destructor part of object, so the func. calling delete this will be last?
    What they mean is don't do this:

    Code:
    someObject->dodelete(); // Delete is called here
    someObject->dosomethingelse(); // Stupid.
    Quote Originally Posted by vb.bajpai View Post
    Naturally the usual caveats apply in cases where your this pointer is a pointer to a base class when you don't have a virtual destructor. - how can a this pointer point to base class ? and if such is a case, is this too polymorphic?
    Frankly, I don't think you should be doing delete this anyway.... There's just too much of a risk to break the code somehow. If you're confused about any aspect, I would recommend you don't do it.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Thanx for the first 2 answers, but the third part still remains unanswered

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by vb.bajpai View Post
    Naturally the usual caveats apply in cases where your this pointer is a pointer to a base class when you don't have a virtual destructor. - how can a this pointer point to base class ? and if such is a case, is this too polymorphic?
    for example,
    Code:
    class Base
    {
    public:
        ~Base() {}
        void DeleteMe() { delete this; }
    };
    
    class Derived : public Base
    {
    public:
        ~Derived() {}
    };
    
    int main() 
    {
        Base *pB = new Derived();
        pB->DeleteMe();  // this is not good.
    }
    In this case, ~Derived() is never called. to fix this you should make ~Base() virtual.

    Quote Originally Posted by MacGyver
    Frankly, I don't think you should be doing delete this anyway.... There's just too much of a risk to break the code somehow. If you're confused about any aspect, I would recommend you don't do it.
    In this specific instance, agreed. No offense, vb.bajpai, but I don't quite think you're ready for this yet.

    That said, as long as you control creation of the object (.i.e. with a private constructor and a static Create() member that ensures it's created with new) 'delete this' is a perfectly valid idiom.
    Last edited by ChaosEngine; 06-25-2007 at 09:04 PM.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Smile

    thank you !


    In this specific instance, agreed. No offense, vb.bajpai, but I don't quite think you're ready for this yet.

    I understand, I m just a C++ Beginner

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> In this case, ~Derived() is never called.
    In practice maybe, but technically it's just undefined behavior. It could have other consequences.

  7. #7
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by Daved View Post
    >> In this case, ~Derived() is never called.
    In practice maybe, but technically it's just undefined behavior. It could have other consequences.
    damnit, I thought it might be undefined behaviour. Should've gotten off my ass and looked it up first...
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Daved View Post
    >> In this case, ~Derived() is never called.
    In practice maybe, but technically it's just undefined behavior. It could have other consequences.
    Wish I had a spec laying around. Are you absolutely sure about this? If it's true, somebody needs to fix the Wikipedia entry:

    C++ does permit objects to "commit suicide" with the construct delete this; this operation, when performed, invalidates the object (and the this pointer). Some C++ authors now consider delete this to be a harmful construct and advise against its use.
    (Note: a closely following paragraph says something about "undefined" -- but it seems that paragraph is in reference to invoking a member function on a null pointer to an object, not the section about "delete this.")

    Also, Googling for "C++ delete this" returns many references to people warning against its use, but nowhere do I see it mentioned that it is actually undefined.

  9. #9
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    brewbuck, "delete this" is perfectly well defined. Daved is referring to my example with a base class and a non-virtual destructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21