this pointer - deletion

Discussion in 'C++' started by srini4vasan@gmail.com, Aug 9, 2007.

  1. Guest

    Can we delete 'this' pointer in C++.
    If so how can we delete this pointer?
    If not why?
    , Aug 9, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > Can we delete 'this' pointer in C++.
    > If so how can we delete this pointer?
    > If not why?
    >

    Why would you want to?

    --
    Ian Collins.
    Ian Collins, Aug 9, 2007
    #2
    1. Advertising

  3. wrote:
    > Can we delete 'this' pointer in C++.
    > If so how can we delete this pointer?
    > If not why?
    >


    Yes, you can delete the this pointer.

    After calling "delete", you must not reference any non static member
    variables or non static functions. Anything that causes you to
    dereference the "this" pointer is bad news.
    Gianni Mariani, Aug 9, 2007
    #3
  4. Neelesh Bodas, Aug 9, 2007
    #4
  5. Ian Collins Guest

    Ian Collins wrote:
    > wrote:
    >> Can we delete 'this' pointer in C++.
    >> If so how can we delete this pointer?
    >> If not why?
    >>

    > Why would you want to?
    >

    Please ignore the above, I misread the question....

    --
    Ian Collins.
    Ian Collins, Aug 9, 2007
    #5
  6. wrote:

    > Can we delete 'this' pointer in C++.
    > If so how can we delete this pointer?
    > If not why?


    Of course you can delete the this pointer inside the method of a class (as
    delete operator/operator delete (please don't flame me for not knowing which of
    the terms is the correct one, I always forget which one means which)) does not
    try to set the passed pointer to Null, you can pass any const pointer into
    delete. Note that this is a technique that is used by the MS MFC library,
    although calling "delete this" is frown upon by some programmers:

    Excerpt from MFC implementation, I hope not to violate against some copyright
    clauses:

    // self destruction
    void CView::postNcDestroy()
    {
    // default for views is to allocate them on the heap
    // the default post-cleanup is to 'delete this'.
    // never explicitly call 'delete' on a view
    delete this;
    }

    This is quite handy for fire-and-forget implementation of Views (special windows
    from the Document/view framework).

    Regards,
    Stuart
    Stuart Redmann, Aug 9, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher Pisz

    safe deletion of pointer, trees

    Christopher Pisz, Sep 3, 2004, in forum: C++
    Replies:
    2
    Views:
    360
    John Harrison
    Sep 4, 2004
  2. Replies:
    10
    Views:
    672
    Chris Torek
    Feb 4, 2005
  3. Aff@n
    Replies:
    1
    Views:
    352
    Ian Collins
    Oct 16, 2006
  4. Marcus

    Pointer deletion question

    Marcus, Feb 16, 2007, in forum: C++
    Replies:
    4
    Views:
    302
    Marcus
    Feb 16, 2007
  5. Edsoncv

    JNI and pointer deletion

    Edsoncv, Apr 23, 2008, in forum: Java
    Replies:
    2
    Views:
    544
    Gordon Beaton
    Apr 24, 2008
Loading...

Share This Page