[C.C++] c++中的this

263 0
Honkers 2025-6-27 04:24:36 来自手机 | 显示全部楼层 |阅读模式

一、c++中为何要有this

1、先来看一个例子

  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. class Student{
  5. private:
  6. char *m_name;
  7. int m_age;
  8. float m_score;
  9. public:
  10. void setage(int m_age);
  11. //声明构造函数
  12. Student(char *name, int age, float score);
  13. //声明普通成员函数
  14. void show();
  15. };
  16. //定义构造函数
  17. Student::Student(char *name, int age, float score){
  18. m_name = name;
  19. m_age = age;
  20. m_score = score;
  21. }
  22. void Student::setage(int m_age){
  23. m_age = m_age;
  24. }
  25. //定义普通成员函数
  26. void Student::show(){
  27. cout<<m_name<<"的年龄是"<<m_age<<",成绩是"<<m_score<<endl;
  28. }
  29. int main(){
  30. //创建对象时向构造函数传参
  31. Student stu("李雷", 5, 92.5f);
  32. stu.setage(10);
  33. stu.show();
  34. //创建对象时向构造函数传参
  35. Student *pstu = new Student("韩梅梅", 15, 96);
  36. pstu->setage(20);
  37. pstu -> show();
  38. return 0;
  39. }
复制代码
  • 输出结果是李雷的年龄是5,成绩是92.5;韩梅梅的年龄是15,成绩是96。可能有些读者感到疑惑,年龄为什么不是10,20呢?
  • 真正的原因是c++变量的作用域问题,函数形参跟你的变量重名,在函数内部,c++会仍然使用函数形参,也就是现在真正的类变量m_age是没有调用到的!

那怎么样才能调用到类变量呢?
2、再看另一个例子

  1. #include <iostream>
  2. using namespace std;
  3. class Student{
  4. private:
  5. char *m_name;
  6. int m_age;
  7. float m_score;
  8. public:
  9. void setage(int m_age);
  10. //声明构造函数
  11. Student(char *name, int age, float score);
  12. //声明普通成员函数
  13. void show();
  14. };
  15. //定义构造函数
  16. Student::Student(char *name, int age, float score){
  17. m_name = name;
  18. m_age = age;
  19. m_score = score;
  20. }
  21. void Student::setage(int m_age){
  22. this->m_age = m_age;
  23. }
  24. //定义普通成员函数
  25. void Student::show(){
  26. cout<<m_name<<"的年龄是"<<m_age<<",成绩是"<<m_score<<endl;
  27. //cout<<this->m_name<<"的年龄是"<<this->m_age<<",成绩是"<<this->m_score<<endl;
  28. }
  29. int main(){
  30. //创建对象时向构造函数传参
  31. Student stu("李雷", 5, 92.5f);
  32. stu.setage(10);
  33. stu.show();
  34. //创建对象时向构造函数传参
  35. Student *pstu = new Student("韩梅梅", 15, 96);
  36. pstu->setage(20);
  37. pstu -> show();
  38. return 0;
  39. }
复制代码
  • 输出结果是李雷的年龄是10,成绩是92.5;韩梅梅的年龄是20,成绩是96
  • 原来这样,笔者会不会有这样的疑问,我们的类Student ,比如创建了stu,pstu,两个对象均调用函数show,那么我们的类是如何知道,我们是哪个对象调用了这个show函数呢?
  • 原来c++在这块,把一个东西隐藏了,对就是所属对象的地址也就是我们的this指针

二、this定义说明

1.定义

  • this 是 C++ 中的一个关键字,也是一个 const 指针,它指向当前对象,通过它可以访问当前对象的所有成员。

  • 所谓当前对象,是指正在使用的对象。例如对于stu.show();,stu 就是当前对象,this 就指向 stu。

2.说明

  • this 只能用在类的内部,通过 this 可以访问类的所有成员,包括 private、protected、public 属性的。
  • 注意,this 是一个指针,要用->来访问成员变量或成员函数。

3.总结

  • this 是 const 指针,它的值是不能被修改的,一切企图修改该指针的操作,如赋值、递增、递减等都是不允许的。
  • this 只能在成员函数内部使用,用在其他地方没有意义,也是非法的。
  • 只有当对象被创建后 this 才有意义,因此不能在 static 成员函数中使用(后续会讲到 static 成员)

三、this总结

  • 只能在“成员函数”中使用
  • this指针类型: 类类型 *const (加const是为了保证,指针的指向不被更改)
  • this指针不存储在对象中,不影响对象大小,且始终指向当前对象
  • this指针是“成员函数”的第一个隐藏参数,由编译器自动给出
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

荣誉红客

关注
  • 4014
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行