Skip to content
Martin Jean-Christio edited this page Oct 25, 2022 · 1 revision

Every object in C++ has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object.

#ifndef CLASS_NAME_H # define CLASS_NAME_H class Sample { public: int foo; Sample(void); ~Sample(void); void bar(void); }; #endif
Sample::Sample(void) {	std::cout << "Constructor called" << std::endl; this->foo = 42;	std::cout << "this->foo: " << this->foo << std::endl; this->bar(); return; }; Sample::~Sample(void) {	std::cout << "Destructor called" << std::endl; return; }; void Sample::bar(void) {	std::cout << "Member function bar called" << std::endl; return; };
// Output Constructor called this->foo: 42 Member function bar called Destructor called 
Clone this wiki locally