Just some random facts about this to complement other answers:
class Foo { public: Foo * foo () { return this; } const Foo * cfoo () const { return this; } }; Foo x;
When the object is const , this type becomes a pointer to const .
class Bar { int x; int y; public: Bar () : x(1), y(2) {} void bar (int x = 3) { int y = 4; std::cout << "x: " << x << std::endl; std::cout << "this->x: " << this->x << std::endl; std::cout << "y: " << y << std::endl; std::cout << "this->y: " << this->y << std::endl; } };
The this pointer can be used to access a member that has been clouded by a function parameter or local variable.
template <unsigned V> class Foo { unsigned v; public: Foo () : v(V) { std::cout << "<" << v << ">" << " this: " << this << std::endl; } }; class Bar : public Foo<1>, public Foo<2>, public Foo<3> { public: Bar () { std::cout << "Bar this: " << this << std::endl; } };
Multiple inheritance will result in different parents having different this values. Only the first inherited parent will have the same this value as the derived object.
jxh
source share