当前位置: 首页 > wzjs >正文

郑州达云通网站建设公司怎么样苏州市建设工程信息网

郑州达云通网站建设公司怎么样,苏州市建设工程信息网,用excel可以做网站,短视频seo营销1.基本概念 非静态成员函数都会默认传递this指针(静态成员函数属于类本身,不属于某个实例对象),方便访问对象对类成员变量和 成员函数。 2.基本使用 编译器实际处理类成员函数,this是第一个隐藏的参数,类…

1.基本概念

非静态成员函数都会默认传递this指针(静态成员函数属于类本身,不属于某个实例对象),方便访问对象对类成员变量和 成员函数。

2.基本使用

编译器实际处理类成员函数,this是第一个隐藏的参数,类型为指向当前对象的指针

void func();             // 表面形式
void func(MyClass* this);  // 实际内部形式

编辑代码

class MyClass {
public:int a;void show() {std::cout << a << std::endl; // 相当于 std::cout << this->a;}
};

编译器处理

void show(MyClass* this) {std::cout << this->a << std::endl;
}

2.1区分参数和类成员变量

类成员函数中变量/函数查找顺序:①当前作用域->②函数参数->③类成员变量/函数(通过this指针查找)->④父类成员->⑤编译器报错。

通过this指针可以区分类成员变量还是传入参数

#include <iostream>using namespace std;class Persion {
public:void SetAge(int age) {this->age = age; // this指针指向调用成员函数的对象本身}void SetHeight(double height) {height = height; // 优先使用局部变量height,未使用this指针}void ShowAge() {cout << "Age: " << age << endl; // 使用this指针访问成员变量}	void ShowHeight() {cout << "Height: " << height << endl; // 使用this指针访问成员变量}int age = 0;double height = 0;
};int main() {Persion p;p.SetAge(25);p.SetHeight(175.5);p.ShowAge();    // 输出: Age: 25p.ShowHeight(); // 输出: Height: 0return 0;
}

ShowAge方法中,先查找函数作用域没有age变量,通过隐藏this指针查找到类成员变量age

2.2链式调用

通过返回this可以实现链式调用

#include <iostream>using namespace std;class Persion {
public://需要使用返回Person的引用的方式,才能实现链式调用,否则会重新创建一个对象Persion& SetAge(int age) {this->age = age; // this指针指向调用成员函数的对象本身return *this; // 返回对象本身的引用}Persion& SetHeight(double height) {this->height = height; // 优先使用局部变量height,未使用this指针return *this; // 返回对象本身的引用}void ShowAge() {cout << "Age: " << age << endl; // 使用this指针访问成员变量}	void ShowHeight() {cout << "Height: " << height << endl; // 使用this指针访问成员变量}int age = 0;double height = 0;
};int main() {Persion p;p.SetAge(25).SetHeight(175.5);p.ShowAge();    // 输出: Age: 25p.ShowHeight(); // 输出: Height: 175.5return 0;
}

3.问题

1.非静态类成员函数中使用的this是类成员变量还是编译器隐式传递的this参数

this指针不是一个类成员变量,类成员函数中使用的this指针是编译器隐式传递的this参数。(这就是为什么静态成员函数里不能使用类成员变量和类成员函数,因为静态成员函数没有this指针参数,在函数内部就无法通过this指针找到对应的实例对象的成员变量以及成员函数)

#include <iostream>using namespace std;class Persion {//this指针是隐含在所有非静态成员函数中的一个指针,指向调用该成员函数的对象,编译器会自动传递this指针//this指针不是类成员变量
public://非静态成员函数编译器会自动传递this指针void setAge(int a) {this->age = a;//通过this指针访问成员变量}//静态成员函数编译器不会自动传递this指针static void getAge() {cout << "age is " << this->age << endl;//报错,静态成员函数没有this指针}int age;
};int main() {Persion p;return 0;
}

2.什么情况自动获取this指针并传递给类成员函数,什么时候需要手动传递?

编译器自动传递this指针:在非静态成员函数内部调用类成员函数和类成员变量时(非静态成员函数本身有隐藏this指针参数)或则通过对象直接调用非静态成员函数时(编译器会根据对象自动生成指向该对象的指针),this指针会被编译器自动传递。

需要手动传递this指针:当将静态成员函数“脱离对象上下文”传递(如给线程或lambda表达式,必须手动指定this。

#include <iostream>
#include <thread>using namespace std;class Persion {
public://需要使用返回Person的引用的方式,才能实现链式调用,否则会重新创建一个对象Persion& SetAge(int age) {this->age = age; // this指针指向调用成员函数的对象本身return *this; // 返回对象本身的引用}Persion& SetHeight(double height) {this->height = height; // 优先使用局部变量height,未使用this指针return *this; // 返回对象本身的引用}void ShowAge() {cout << "Age: " << age << endl; // 使用this指针访问成员变量}	void ShowHeight() {cout << "Height: " << height << endl; // 使用this指针访问成员变量}void testThisPoint() {std::thread t1(&Persion::ShowAge, this); // 传递this指针,必须显示传递类成员函数ShowAge第一个参数thisstd::thread t2([this]() { ShowHeight(); }); // 使用lambda表达式捕获this指针,表示式作用域内找不到ShowHeight方法,再通过this指针访问t1.join();t2.join();std::cout << t1.joinable() << std::endl;std::cout << t2.joinable() << std::endl;}int age = 0;double height = 0;
};int main() {Persion p;p.SetAge(25).SetHeight(175.5);p.testThisPoint();return 0;
}

3.成员函数如何区分使用变量是类成员变量还是未定义变量或则传入参数变量?this指针工作原理

层层查找,和普通变量作用域一样,从内到外层层查找

①当前作用域->②函数参数->③类成员变量/函数(通过this指针查找)->④父类成员->⑤编译器报错。

4.总结

1.this指针不是一个类成员变量,类成员函数中使用的this指针是编译器隐式传递的this参数。


文章转载自:

http://z0CB9GOR.nmymn.cn
http://AJ5pwD25.nmymn.cn
http://q1Q5sc7k.nmymn.cn
http://5z3wKXPw.nmymn.cn
http://o1t4HAwi.nmymn.cn
http://ahoDsfzS.nmymn.cn
http://JoGgn5A5.nmymn.cn
http://j5R38VpJ.nmymn.cn
http://LfEXBBte.nmymn.cn
http://tICN7PRy.nmymn.cn
http://6noG7Xyh.nmymn.cn
http://ts33IK86.nmymn.cn
http://0HaRVkmW.nmymn.cn
http://tbc4e8tv.nmymn.cn
http://4OYO16rI.nmymn.cn
http://xrUGE6o1.nmymn.cn
http://dXQnnNzx.nmymn.cn
http://no3SZ4k7.nmymn.cn
http://JAKvgaik.nmymn.cn
http://CvAEBFJj.nmymn.cn
http://4lxX41VM.nmymn.cn
http://DiKhCULE.nmymn.cn
http://Cq0h3FXK.nmymn.cn
http://dMEyc6nj.nmymn.cn
http://FLz09R87.nmymn.cn
http://iAtJWYwG.nmymn.cn
http://8UldaL8M.nmymn.cn
http://U5w2ZqRr.nmymn.cn
http://r0PB15KW.nmymn.cn
http://DJznRZ2R.nmymn.cn
http://www.dtcms.com/wzjs/733678.html

相关文章:

  • 牛商网网站建设多少钱网站开发流程详细介绍
  • 网站ftp地址查询苏州本地网站有哪些
  • 更改各网站企业信息怎么做安阳做网站哪家好
  • 无锡企业网站排名优化网站设计不包括
  • 新的网站设计公司工商服务平台
  • 网站二级目录打不开几分钟做网站
  • 公司网站上传图库投资建设集团网站
  • 潍坊个人做网站旅游网站开发毕业设计论文
  • 网站 开发 价格网页布局设计技术包括
  • 建站魔方极速网站建设做网站用什么语言快
  • 仙居做网站在哪里做中国数控机床网
  • 汽车配件响应式网站家装公司加盟哪个公司好
  • 做设备开通哪个网站好宁波大型网站推广服务
  • 电商网站的模块网站利润
  • 河南手机网站建设多少钱北海网站设计公司
  • 服务号开发随州seo
  • 长沙网站制作培训商城网站建设开发多少钱
  • 网站特色怎么写广州室内设计公司排行榜
  • 网站后台发布文章横琴网站建设公司
  • 番禺建网站免费代理网址
  • 网站不备案可以么广州网站建设报价单
  • 沈阳网站页面设计公司建站赚钱灰色
  • 怎么使网站降权肇庆市场核酸检测
  • 中国制造网官方网站国际站wordpress 密码解密
  • 网站开发是做什么google seo实战教程
  • 大型电子商务网站建设试述网站建设应考虑哪些方面的问题
  • 企业的建站方式中国机械工业建设集团有限公司网站
  • 网站短信通知wordpress将404跳转主页
  • 医院网站建设价格ftp怎么上传网站
  • 架设网站 自己购买服务器网站建设成本图