“When a member function is called, how does C++ keep track of which object it was called on?”. The answer is that C++ utilizes a hidden pointer named “this”!
“當(dāng)調(diào)用成員函數(shù)時(shí),C++如何跟蹤調(diào)用它的對(duì)象?”。答案是C++使用了一個(gè)名為“this”的隱藏指針!
#include class Pointer{ int x,y,z; public: static double pi; Pointer(int a,int b,int c):x(a),y(b),z(c){} int getx(){ // 隱含const this指針,引用當(dāng)前類(lèi)的實(shí)例變量。 return x; } int gety() const; // 隱含const int* const this指針 int getz() const{ // 隱含const int* const this指針 return this->z; } Pointer& operator=(Pointer& others) // 隱含const this指針 { x = others.x; y = others.y; this->z = others.z; return *this; } static double getpi() // no this pointer { return pi; }};double Pointer::pi = 3.141592;int Pointer::gety() const{ // 隱含const int* const this指針 return this->y;}int gety(const Pointer *ths){ return ths->gety();}int main(){ Pointer pt(3,4,5); printf(“%d”,pt.gety()); // 4 printf(“%d”,gety(&pt)); // 4 printf(“%f”,Pointer::getpi());// 3.141592 getchar();}
When overloading an operator using a member function:
使用成員函數(shù)重載運(yùn)算符時(shí):
The overloaded operator must be added as a member function of the left operand.
重載運(yùn)算符必須作為左操作數(shù)的成員函數(shù)添加。
The left operand becomes the implicit *this object
左操作數(shù)成為隱式*this對(duì)象
All other operands become function parameters.
所有其他操作數(shù)都成為函數(shù)參數(shù)。
#include class Cents{private: int m_cents;public: Cents(int cents) : m_cents(cents) { } // Overload Cents + int Cents operator+ (int value); // implicitly const this int getCents() const { return m_cents; }};// note: this function is a member function!// the cents parameter in the friend version is now the implicit *this parameterCents Cents::operator+ (int value) // implicitly const this{ return Cents(m_cents + value); //this->m_cents}int main(){Cents cents1(6);Cents cents2(cents1 + 2);std::cout << "I have " << cents2.getCents() << " cents."; // I have 8 cents. getchar();return 0;}
It can sometimes be useful to have a class member function return the object it was working with as a return value. The primary reason to do this is to allow a series of member functions to be “chained” together, so several member functions can be called on the same object!
有時(shí),讓類(lèi)成員函數(shù)將其處理的對(duì)象作為返回值返回會(huì)很有用。這樣做的主要原因是允許將一系列成員函數(shù)“鏈接”在一起,以便可以在同一對(duì)象上調(diào)用多個(gè)成員函數(shù)!
By having functions that would otherwise return void return *this instead, you can make those functions chainable. This is most often used when overloading operators for your classes.
通過(guò)使用返回*this代替返回void的函數(shù),您可以使這些函數(shù)可鏈接。這在重載類(lèi)的運(yùn)算符時(shí)最常用。
#include class Calc{private: int m_value;public: Calc(int a):m_value(a){}; Calc& add(int value) { m_value += value; return *this; } Calc& sub(int value) { m_value -= value; return *this; } Calc& mult(int value) { m_value *= value; return *this; } int getValue() { return m_value; }};int main(){ Calc calc(0); calc.add(5).sub(3).mult(4); std::cout << calc.getValue() << ''; // 8 getchar();}
ref
https://www.learncpp.com/cpp-tutorial/the-hidden-this-pointer/
_End_