#include <iostream>
#include <string>
using namespace std;
// 定义一个类 Student
class 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;}
void Show()
{ cout<<"this = "<<this<<endl;}
protected:
};
int main(void)
{
Student x;
x.Show();
cout<<"&x = "<<&x<<endl;
// 以上表明this指向的就是当前的对象x
cout<<"----------------------"<<endl;
Student y;
y.Show();
cout<<"&y = "<<&y<<endl;
// 以上表明this指向的就是当前的对象y
// --------------------
return 0;
}
第一次调用时,this地址与对象x的一样;
第二次调用是,this地址与对象y的一样.