C++ 类的 this 指针 语法练习3
来源:互联网 发布:java 开启定时器 编辑:程序博客网 时间:2024/06/05 11:22
#include <iostream>#include <string>using namespace std;// 定义一个类 Studentclass Student{private:string name;int age;string address;public:// 存void setname(string s){name = s;}void setage(int y){age = y;}void setaddress(string add){address = add;} // 取string getname(){return name;}int getage(){return age;}string getaddress(){return address;} // 返回 指向Student 对象的指针,因为this是指针,且是指向当下对象的指针Student* Show(){cout<<"姓名: "<<name<<" --- 年龄: "<<age<<" ---- 住址: "<<address<<endl; return this;} protected:};int main(void){ Student x; x.setname("柳絮飘"); x.setage(22); x.setaddress("学明路115号"); // 类的成员函数Show返回的是this指针, // x.Show()并没有用一个指针变量来接受它的返回值就直接用了. // 有返回值的函数,调用时并不一定要搞个变量接受它的返回值. x.Show()->Show()->Show()->Show(); // cout<<"对象x的地址 = "<<&x<<endl; cout<<"--------------------------------------------------"<<endl; // 搞个指针变量来接受吧. Student * y =NULL; y = x.Show(); cout<<"指针y的地址 = "<<y<<endl; y->setname("张生民"); y->setage(23); y->setaddress("琴海路136号"); y->Show()->Show()->Show();// --------------------return 0;}
//--
从结果看,对象x的地址与指针y的地址是一样的, 说明show返回的指针this 是指向当前对象x的.
- C++ 类的 this 指针 语法练习
- C++ 类的 this 指针 语法练习3
- C++ 类的 this 指针 语法练习2
- C++ 类的 this 指针 语法练习4
- C++ 类的 this 指针 语法练习5
- C ++的this指针
- 【c++】this 指针和类的继承
- C++, 类的构造函数 语法练习
- C++this指针的理解
- C++this指针的理解
- C++this指针的用法
- 【c++】this指针的使用
- 【C++】隐含的this指针
- C语言指针的练习
- 类的this指针
- C++Primer学习-类(隐含的this指针)
- 【C++】谈谈类定义里的this指针
- 【C++】【学习笔记】【016】this指针和类的继承
- 线段树——Powered By God horses are clouds
- 博弈——Powered By God horses are clouds
- 线段树——Powered By 漂泊是为了更好的停驻
- 搜索——Powered By 漂泊是为了更好的停驻
- C++ 类的 this 指针 语法练习2
- C++ 类的 this 指针 语法练习3
- 2012杭州网络赛赛后【缺ACDI】
- 2012金华网络赛赛后【缺ABC】
- 2012成都网络赛赛后【缺CHJ】
- C++ 类的 this 指针 语法练习4
- hdu_4417 Super Mario (树状数组)
- C++ 类的 this 指针 语法练习5
- 把数组排成最小的数
- 扑克牌的顺子