The this pointer is not a member of the class. This is just a construct that is used in methods belonging to the class to access the current instance.
If you have a class like this:
class IntPair { public: IntPair(int a, int b) : _a(a), _b(b) { } int sum() const { return _a + _b; } public: int _a; int _b; };
This class only requires space for two int instances for each instance. After you create an instance and use the sum() method, this method is called with a pointer to the instance, but this pointer always comes from somewhere else, it is not stored in the object instance.
For example:
IntPair *fib12 = new IntPair(89, 144); cout << fib12->sum();
Note that the variable that becomes the this pointer is stored outside the object, in the area that created it.
You could actually convert a method similar to the above:
static int sum2(const IntPair* instance) { return instance->_a + instance->_b; }
If the above is defined inside the class (so that it can access private members), there is no difference. In fact, this is exactly how the methods are implemented behind the scenes; the this pointer is just a hidden argument for all member methods.
Call:
IntPair* fib12 = new IntPair(89, 144); cout << IntPair::sum2(fib12);
unwind
source share