Home >Backend Development >C++ >Detailed explanation of the usage of this pointer in C++
This pointer is a special pointer in C, pointing to the current object instance, used to access member variables, call member functions, pass to other functions, restrict access to members, and distinguish it from other pointers.
Usage of this pointer in C
What is this pointer?
This pointer is a special pointer pointing to the current object instance.
Usage of this pointer
<code class="cpp">class Person { public: string name; Person(string name) : name(name) {} void printName() { cout << this->name << endl; } };</code>
<code class="cpp">class Person { public: string name; Person(string name) : name(name) {} void printName() { this->printName(); } };</code>
<code class="cpp">void printPerson(Person* person) { cout << person->name << endl; }</code>
<code class="cpp">class Person { public: string name; Person(string name) : name(name) {} void const printName() const { cout << this->name << endl; } };</code>
When to use this pointer?
You usually need to use this pointer in the following situations:
The above is the detailed content of Detailed explanation of the usage of this pointer in C++. For more information, please follow other related articles on the PHP Chinese website!