//this指针有时候可以用,有时候必须用
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(const std::string &nm,const std::string &addr)
{
this->name = nm;//this是一个指针,通过这个指针来使用name,
this->address = addr;//this可以写可以不写,最好写
}
std::string getName() const
{
return this->name;//this表示这个address是私有成员里的address
}
std::string getAddress() const
{
return this->address;
}
private:
std::string name;
std::string address;
};
int main()
{
Person p("hello", "worls");
cout << p.getName() << endl;
system("pause");
return 0;
}
this指针
最新推荐文章于 2025-03-06 17:06:56 发布