亚洲精品中文免费|亚洲日韩中文字幕制服|久久精品亚洲免费|一本之道久久免费

      
      

            <dl id="hur0q"><div id="hur0q"></div></dl>

                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”!

                “當調(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指針,引用當前類的實例變量。 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),不代表本站立場,版權歸原作者所有,如有侵權請聯(lián)系管理員(admin#wlmqw.com)刪除。
                用戶投稿
                上一篇 2022年6月21日 15:16
                下一篇 2022年6月21日 15:16

                相關推薦

                • 存儲過程語法(sql server存儲過程語法)

                  今天小編給各位分享存儲過程語法的知識,其中也會對sql server存儲過程語法進行解釋,如果能碰巧解決你現(xiàn)在面臨的問題,別忘了關注本站,現(xiàn)在開始吧! oracle存儲過程基本語法…

                  2022年11月26日
                • 英雄聯(lián)盟暗裔傳說11.24答案分享 暗裔傳說第四天答案攻略

                  英雄聯(lián)盟手游能力者·叁-暗裔傳說第四天答案是什么?暗裔傳說11月24日是活動的第四天,今天的題目也更新了三個新問題,題目和答案小編已經(jīng)分享在下面,大家直接通過攻略就可以了解到暗裔傳…

                  2022年11月25日
                • 寶可夢朱紫面試答案大全 面試題目答案全匯總

                  寶可夢朱紫面試答案是什么?打敗四大天王之后就能去完成聯(lián)盟挑戰(zhàn)成為冠軍,后續(xù)還會有聯(lián)盟面試,在面試的時候大家會遇到很多的問題需要回答,一共有十道題目,這些題目的正確答案小編這就分享在…

                  2022年11月25日
                • 博客營銷的3大優(yōu)勢解析(博客營銷怎么做)

                  不知不覺已經(jīng)寫了24篇文章,加上這篇是第25篇了,都是自己這幾年來用過的營銷方法,如果遇到有些不懂的,我會咨詢我的朋友和同事幫忙,盡量讓每一篇有價值,哪怕是對大家有一點點幫助也行,…

                  2022年11月25日
                • 英雄聯(lián)盟手游暗裔傳說11.24答案 LOL手游能力者·叁-暗裔傳說第四天答案

                  英雄聯(lián)盟手游能力者·叁-暗裔傳說第四天答案是什么?暗裔傳說11月24日是活動的第四天,今天的題目也更新了三個新問題,題目和答案小編已經(jīng)分享在下面,大家直接通過攻略就可以了解到暗裔傳…

                  2022年11月24日
                • 淘寶直播開通后帶貨鏈接怎么做(淘寶直播需要開通淘寶店鋪嗎)

                  直播帶貨無論是對于商家來說還是主播收益都是非常可觀的,所以不少平臺都有直播帶貨功能,一些小伙伴也想加入淘寶直播,那么淘寶直播開通后帶貨鏈接怎么做?下面小編為大家?guī)硖詫氈辈ラ_通后帶…

                  2022年11月24日
                • 《寶可夢朱紫》四天王面試答案匯總 四天王測試答案介紹

                  寶可夢朱紫四天王面試答案是什么?在游戲中,玩家挑戰(zhàn)四天王的話,需要進行面試測試,有很多題目需要玩家完成,很多玩家還不清楚寶可夢朱紫四天王面試答案是什么,下面一起來看一下寶可夢朱紫四…

                  2022年11月23日
                • 快手限流多久能解除(快手限流什么意思)

                  我相信很多人都看中了快手平臺的商機,都爭先恐后地想要搶占機會,可一些人剛剛作出一點成績,就被降權了,自己也不知道什么原因。所以今天就來聊聊快手賬號降權操作分享,趕快來看看避免違規(guī)!…

                  2022年11月23日
                • 《寶可夢朱紫》歷史課答案是什么?歷史課答案一覽

                  寶可夢朱紫中玩家作為學校的學生自然是要學習和參加考試的,很多玩家想知道寶可夢朱紫歷史課答案是什么,下面就帶來寶可夢朱紫歷史課答案一覽,感興趣的小伙伴一起來看看吧,希望能幫助到大家?!?/p>

                  2022年11月22日
                • 英雄聯(lián)盟手游暗裔傳說答案11.22 當凱隱處于藍凱隱狀態(tài)時,3技能將?

                  英雄聯(lián)盟手游暗裔傳說11.22答案是什么?暗裔傳說答題每天都會有三道題目更新,今天的題目詳情小編已經(jīng)分享在下面,想要了解這些題目的正確答案,那么就多來小編這里看一看暗裔傳說第二天的…

                  2022年11月22日

                聯(lián)系我們

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