操作符*或者->的重载

来源:互联网 发布:小米笔记本 显卡知乎 编辑:程序博客网 时间:2024/05/22 18:32

一般操作符*返回的是对象的引用 ,而->返回的是对称的指针

以Test类为例

class Test{public:Test() :a(0), b(0) {}Test& operator*(){cout << "operator*" << endl;return *this;}Test* operator->(){cout << "opertator->" << endl;return this;}int a, b;};

测试例子为:

Test t;cout << t->a << endl;cout << (*t).a << endl;

输出为

operator->

0

operator*

0

原创粉丝点击