免费爱碰视频在线观看,九九精品国产屋,欧美亚洲尤物久久精品,1024在线观看视频亚洲

      C++|成員函數(shù)與this指針

      “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ù)時,C++如何跟蹤調(diào)用它的對象?”。答案是C++使用了一個名為“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)前類的實例變量。 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ù)重載運算符時:

      The overloaded operator must be added as a member function of the left operand.

      重載運算符必須作為左操作數(shù)的成員函數(shù)添加。

      The left operand becomes the implicit *this object

      左操作數(shù)成為隱式*this對象

      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ù)將其處理的對象作為返回值返回會很有用。這樣做的主要原因是允許將一系列成員函數(shù)“鏈接”在一起,以便可以在同一對象上調(diào)用多個成員函數(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.

      通過使用返回*this代替返回void的函數(shù),您可以使這些函數(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_

      鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場,版權(quán)歸原作者所有,如有侵權(quán)請聯(lián)系管理員(admin#wlmqw.com)刪除。
      用戶投稿
      上一篇 2022年6月21日 15:16
      下一篇 2022年6月21日 15:16

      相關(guān)推薦

      聯(lián)系我們

      聯(lián)系郵箱:admin#wlmqw.com
      工作時間:周一至周五,10:30-18:30,節(jié)假日休息