C++|成员函数与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”!

“当调用成员函数时,C++如何跟踪调用它的对象?”。答案是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:

使用成员函数重载运算符时:

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

重载运算符必须作为左操作数的成员函数添加。

The left operand becomes the implicit *this object

左操作数成为隐式*this对象

All other operands become function parameters.

所有其他操作数都成为函数参数。

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

有时,让类成员函数将其处理的对象作为返回值返回会很有用。这样做的主要原因是允许将一系列成员函数“链接”在一起,以便可以在同一对象上调用多个成员函数!

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的函数,您可以使这些函数可链接。这在重载类的运算符时最常用。

#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

13.10 — The hidden “this” pointer

_End_

郑重声明:本文内容及图片均整理自互联网,不代表本站立场,版权归原作者所有,如有侵权请联系管理员(admin#wlmqw.com)删除。
(0)
用户投稿
上一篇 2022年6月21日
下一篇 2022年6月21日

相关推荐

  • 人到中年 Middle age

    人到中年,我多了一些矜持下的沉重,看孩子欢蹦乱跳在膝前,常追忆自己那远去的辛酸的童年; 人到中年,我多了一些成熟后的自律,看父母一天天的老去,有时常感叹自己为啥不能多留点爱在他们身…

    2022年6月20日
  • 如何才能正确的学习电脑技术?

    想学好办。到电脑公司买一台,告诉服务员:就说小白一个,教会操作方法才能买,你就什么都学到了。付钱搬电脑回家。 看了几个回答,感觉根本不靠谱,为了避免大家被误导,特此回答。 在没有电…

    2022年4月12日
  • 文案|影为光而生,光却为影子而在

    1、影为光而生,光却为影子而在.记得中学时有个问题问着,什么时候的影子最长,是早晨太阳升起时,也是每天黄昏日落时,还有我望着你背影的时候.——网易云热评《追光者》 2、十年前你说生…

    2022年6月9日
  • 番茄金汤肥牛,香喷喷,超下饭

    要是想吃肥牛,在家这么多,操作简单不复杂,学会的,你的家人都会夸你是大厨。 首先肥牛先过水,这样子不会骚,而且汤汁也不会变黑,这个步骤你可不要省了,锅中下油,把葱姜蒜炒出香味后,下…

    2022年8月25日
  • 半百传奇全新出击!GRAND SEIKO 44GS 55周年纪念表款

    众所皆知,Grand Seiko不只是SEIKO旗下最高等级的代表,也是他们对于「如何把一只表做到极致」的答案。这个答案背后当然包含了壳型设计、打磨、指针与时标切割、抛光等等细节处…

    2022年8月22日
  • LPL夏季赛LGD迷惑操作看呆众人,水晶哥完全诠释短短五秒

    对于绝大多数经常观看比赛的英雄联盟玩家们来讲,肯定都对目前正在火热进行中的2022年LPL夏季赛都不陌生了,LPL夏季赛作为国内英雄联盟游戏圈中含金量最高,最具观赏性的一场职业联赛…

    2022年7月21日
  • 面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六

    不知不觉,面渣逆袭系列已经肝了差不多十篇,每一篇都是上万字,几十图,基本上涵盖了面试的主要知识点,这期MySQL结束之后,这个系列可能会暂时告一段落,作为面渣逆袭系列第一阶段的收官…

    2022年6月23日
  • 在A股市场“活下去”的秘诀

    2018年秋季,万科在内部会议上喊出“活下去”的口号,结果房地产不行了。2018和2019年高位接盘的购房客,或许已经感受到山顶的寒气,内心是无比的煎熬。 2022年,任正非一句“…

    2022年8月26日
  • 前端如何卷后端

    有很多前端小伙伴问我(博主:最爱白菜吖),如何学后端?他们觉得后端每天curl写个API很轻松,自己天天写页面,还要和后端撕逼,想卷后端。下面我就给大家简单的介绍一下,前端如何卷后…

    2022年7月9日
  • 新能源充电桩现在到底能不能做 答案是肯定的 但是这几点必须要考虑

    根据预测,我国到2025年新能源汽车的零售渗透率将达到35%,年销量预计将超过1000万辆,现保有量将达到3700万辆。同时很多有投资眼光的朋友看到了这背后巨大的商机。那么新能源汽…

    2022年6月25日

联系我们

联系邮箱:admin#wlmqw.com
工作时间:周一至周五,10:30-18:30,节假日休息