The "this" Pointer.

closed account (zb0S216C)
Is it possible to delete a class member that allocates heap memory using the "this" pointer?. Take the following code segment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

class MY_CLASS
{
    // Constructor.
    MY_CLASS( int nInit )
    {
        // Allocate memory from the heap.
        this -> m_pMyVariable = new int( nInit );
    }

    // Destructor.
    ~MY_CLASS( void )
    {
        if( this -> m_pMyVariable != NULL )
        {
            delete this -> m_pMyVariable;
        }
    }
    int *m_pMyVariable;

};


So, my question is: Can I delete a class member using this line of code?:
 
delete this -> m_pMyVariable;


Thanks.
Yes you can do that, and that's exactly what you're supposed to do.

Although note that the this-> in all of that code is redundant and unnecessary. You can just type m_pMyVariable and it will have the same effect.
closed account (zb0S216C)
Thanks Disch.
Topic archived. No new replies allowed.