C++ this指针详解(精辟)
生活随笔
收集整理的這篇文章主要介紹了
C++ this指针详解(精辟)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
link
this 是 C++ 中的一個關鍵字,也是一個 const 指針,它指向當前對象,通過它可以訪問當前對象的所有成員。所謂當前對象,是指正在使用的對象。例如對于stu.show();,stu 就是當前對象,this 就指向 stu。
下面是使用 this 的一個完整示例:
#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++拷贝构造函数(复制构造函数)详解
- 下一篇: Clion 快捷键