class's pointer to its own adress ?


I have this class:

class _test
*_test OwnPointer;

//some constructor code here

How can I make the constructor access its own class pointer ?

So each time I would create a new class of type _test, it would automatically fill OwnPointer with its own adress (that is, the beginning of _test class).

thanks !

In C++ there is the idea of “this”, accessible from within member functions of a class (not static ones though, for obvious reasons), which refers to the current object. It’s a pointer to the same type as the class.


// This is C++
class CClass
        // accessing members via "this"
        this->m_value = 100;

        // printing the address of the object
        printf( "My address = %x
", this );

    int m_value;

So, you don’t need to store the address of the object anywhere, just use “this” where ever you need it.

thanks a lot !