C++官方文档-this
#include <iostream> using namespace std;class Dummy { public:int x;Dummy(): x(0){};Dummy(int xx): x(xx){};bool isitem(Dummy const&);Dummy& operator=(Dummy const& param); }; bool Dummy::isitem(Dummy const& param) {if(this == ¶m)return true;return false; } Dummy& Dummy::operator=(const Dummy& param) {this->x = param.x;cout << "=" << endl;//this指向等号左边的对象return *this; }int main() {Dummy a;Dummy* b = &a;if(b->isitem(a))cout << true << endl;elsecout << false << endl;Dummy foo(3);Dummy bar;//声明一个对象,然后把foo赋值给它 // Dummy bar=foo;//调用重载函数= // bar.operator =(foo);//调用重载函数=bar = foo;cout << bar.x << endl;cout << bar.isitem(foo) << endl;cout << bar.isitem(bar) << endl;return 0; }
posted on 2018-02-14 16:04 好吧,就是菜菜 阅读(...) 评论(...) 编辑 收藏