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

c++(类) this指针

this指针的相关概念:

          this只能在成员函数中使用。全局函数,静态函数都不能使用this。实际上,成员函数默认第一个参数为T* const register this。

          为什么this指针不能再静态函数中使用?

          大家可以这样理解,静态函数如同静态变量一样,他不属于具体的哪一个对象,静态函数表示了整个类范围意义上的信息,而this指针却实实在在的对应一个对象,

           所以this指针当然不能被静态函数使用了,同理,全局函数也一样,我是这样理解的,不知道大家怎样理解,大家可以评论交流下。

 

(1)this指针是什么时候创建的?

            this在成员函数的开始执行前构造的,在成员的执行结束后清除。

 

(2)this指针如何传递给类中函数的?绑定?还是在函数参数的首参数就是this指针.那么this指针又是如何找到类实例后函数的?

            this是通过函数参数的首参数来传递的。this指针是在调用之前生成的。类实例后的函数,没有这个说法。类在实例化时,只分配类中的变量空间,并没有为函数分配空间。

            自从类的函数定义完成后,它就在那儿,不会跑的。

 

(3)this指针只有在成员函数中才有定义。因此,你获得一个对象后,也不能通过对象使用this指针。所以,我们也无法知道一个对象的this指针的位置(只有在成员函数

            里才有this指针的位置)。当然,在成员函数里,你是可以知道this指针的位置的(可以&this获得),也可以直接使用的。

 

 

this指针的使用:

       一种情况就是,在类的非静态成员函数中返回类对象本身的时候,我们可以使用圆点运算符(*this).,箭头运算符this->,另外,我们也可以返回

关于*this的引用,这样我们可以像输入输出流那样进行“级联”操作。

示例如下:

1:(普通)

 1 #include "stdafx.h"
 2 #include<iostream>  
 3 #include<string>  
 4 using namespace std;
 5 class Stu_Info_Mange
 6 {
 7     int sno;
 8     string sname;
 9     int age;
10     int grade;
11 public:
12     Stu_Info_Mange(int s = 0, string n = "none", int a = 0, int g = 0)
13     {
14         sno = s;
15         sname = n;
16         age = a;
17         grade = g;
18     }
19     void Setsname(std::string sn)   //使用this指针进行赋值  
20     {
21         this->sname = sn;
22     }
23     int  Setage(int a)
24     {
25         this->age = a;
26         return (*this).age; //使用this指针返回该对象的年龄  
27     }
28     void show()
29     {
30         cout << "the sname is " << this->sname << endl;  //显式this指针通过箭头操作符访问  
31         cout << "the sno   is " << sno << endl;//隐式使用this指针打印  
32         cout << "the age   is " << (*this).age << endl;//显式使用this指针通过远点操作符  
33         cout << "the grade is " << this->grade << endl << endl;
34     }
35 
36 };
37 int main()
38 {
39     Stu_Info_Mange p1(761, "张三", 19, 3);
40     p1.show();  //输出信息 
41     p1.Setage(12); //使用this指针修改年龄 
42     p1.Setsname("王五");
43     p1.show();     //再次输出  
44     return 0;
45 }

 

2:(使用类)

Stu_Info_Mange.h

 1 #pragma once
 2 #include<string>
 3 using std::string;
 4 class Stu_Info_Mange
 5 {
 6 private:
 7     int sno;
 8     string sname;
 9     int age;
10     int grade;
11 public:
12     Stu_Info_Mange(int s = 0, string n = "none", int a = 0, int g = 0);
13     void Setsname(string sn) { this->sname = sn; }
14     int  Setage(int a);
15     void show()const;
16     ~Stu_Info_Mange();
17 };

 

Stu_Info_Mange.cpp

 1 #include "stdafx.h"
 2 #include "Stu_Info_Mange.h"
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 Stu_Info_Mange::Stu_Info_Mange(int s, string n, int a, int g)
 7 {
 8     sno = s;
 9     sname = n;
10     age = a;
11     grade = g;
12 }
13 
14 int Stu_Info_Mange::Setage(int a)
15 {
16     this->age = a;
17     return (*this).age;
18 }
19 
20 void Stu_Info_Mange::show() const
21 {
22     cout << "the sname is " << this->sname << endl;
23     cout << "the sno   is " << sno << endl;
24     cout << "the age   is " << (*this).age << endl;
25     cout << "the grade is " << this->grade << endl << endl;
26 }
27 
28 Stu_Info_Mange::~Stu_Info_Mange()
29 {
30 }

ConsoleApplication.cpp

 1 #include "stdafx.h"
 2 #include "Stu_Info_Mange.h"
 3 #include<iostream>
 4 #include<string>
 5 using namespace std;
 6 int main()
 7 {
 8     Stu_Info_Mange p1(761, "张三", 19, 3);
 9     p1.show();  //输出信息 
10     p1.Setage(12); //使用this指针修改年龄 
11     p1.Setsname("王五");
12     p1.show();     //再次输出  
13     return 0;
14 }

 

输出结果:

转载于:https://www.cnblogs.com/Trojan00/p/8894380.html

相关文章:

  • 【Python改变生活!】用pynput控制键盘鼠标!Mac如何卸载python?
  • Docker 镜像、容器、仓库的概念及基本操作
  • 十分钟讲清楚大众对区块链的误解
  • 移动端适配问题解决方案
  • 每天一个linux命令(20):find命令之exec
  • 多张图片合成一张图片、兼容问题总结
  • git 配置多个账户
  • CentOS下安装php扩展exif
  • Alembic基本使用
  • 机器学习中特征的处理及选择
  • Prometheus Querying Function rate() vs irate()
  • 多线程基础篇(3)——初试锁
  • java获取文件列表,并按照目录的深度及文件名的拼音的升序排列
  • 爬虫大作业
  • 散列表解决冲突的办法
  • 【comparator, comparable】小总结
  • Angular Elements 及其运作原理
  • angular学习第一篇-----环境搭建
  • DataBase in Android
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • mysql 数据库四种事务隔离级别
  • spring boot下thymeleaf全局静态变量配置
  • 安装python包到指定虚拟环境
  • 初识 beanstalkd
  • 关于 Cirru Editor 存储格式
  • 每天一个设计模式之命令模式
  • 如何编写一个可升级的智能合约
  • 项目实战-Api的解决方案
  • 用mpvue开发微信小程序
  • 用Visual Studio开发以太坊智能合约
  • Android开发者必备:推荐一款助力开发的开源APP
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • #ubuntu# #git# repository git config --global --add safe.directory
  • (11)工业界推荐系统-小红书推荐场景及内部实践【粗排三塔模型】
  • (2)STM32单片机上位机
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (JS基础)String 类型
  • (PHP)设置修改 Apache 文件根目录 (Document Root)(转帖)
  • (react踩过的坑)antd 如何同时获取一个select 的value和 label值
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (循环依赖问题)学习spring的第九天
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转)Groupon前传:从10个月的失败作品修改,1个月找到成功
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .net开发时的诡异问题,button的onclick事件无效
  • .net连接MySQL的方法
  • .so文件(linux系统)
  • @angular/cli项目构建--http(2)
  • @DependsOn:解析 Spring 中的依赖关系之艺术
  • @RequestMapping处理请求异常
  • @Service注解让spring找到你的Service bean
  • [ 云计算 | AWS 实践 ] Java 如何重命名 Amazon S3 中的文件和文件夹
  • [120_移动开发Android]008_android开发之Pull操作xml文件