实例指针(this)

最后更新于:2022-04-02 02:07:27

[TOC] ## 参数与成员变量重名 可使用 `this->` 做处理 ``` class A { public: int a; void setData(int a) { this->a=a; }; void getData() { cout << this->a<< endl; }; }; ``` ## 判断一个对象是否为自己
main.cpp ``` #include #include using namespace std; class A { public: bool check(A *obj) { if (obj == this) { cout << "is self" << endl; } else { cout << "is not self" << endl; } } }; class B :public A { public: A *getBase() { A *p = this; return p; } }; int main() { A a; B b; A *p1 =&a; A *p2=b.getBase(); a.check(p1); a.check(p2); return 0; } ```

输出 ``` is self is not self ``` ## 运算符重载
main.cpp ``` ```

';