我是靠谱客的博主 长情乌冬面,这篇文章主要介绍C++ this指针详解(精辟),现在分享给大家,希望可以做个参考。

link

this 是 C++ 中的一个关键字,也是一个 const 指针,它指向当前对象,通过它可以访问当前对象的所有成员。所谓当前对象,是指正在使用的对象。例如对于stu.show();,stu 就是当前对象,this 就指向 stu。
下面是使用 this 的一个完整示例:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream> using namespace std; class Student{ public: void setname(char *name ); void setage( int age ); void setmoney(float score ); void show(); private: char * name; int age ; float score; }; void Student::setname(char *name ) { this->name =name; } void Student::setage(int age ) { this->age =age; } void Student::setmoney(float score ) { this->score =score; } void Student::show() { cout<<this->name<<"的年龄是"<<this->age<<",的身价"<<this->score<<"亿"<<endl; } int main() { Student *pstu=new Student; pstu ->setname("罗干"); pstu->setage( 33); pstu->setmoney(1); pstu ->show(); return 0 ; }

最后

以上就是长情乌冬面最近收集整理的关于C++ this指针详解(精辟)的全部内容,更多相关C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(106)

评论列表共有 0 条评论

立即
投稿
返回
顶部