c++ *运算符重载

运算符重载,对象和指向对象的指针

直接上code

复制代码 代码如下:

#include <iostream>
using namespace std;
 class test
{
    public:
        int a;
        test() : a(0){}
        test &operator*(){
            cout << "operator*" << endl;
            cout << a << endl;
            return *this;
        }
};
 
 int main()
{
    test *t;
    t = new test;
    test t2 = *t;
    t->a += 1;
    // t2.a += 1;
    *t = *t2;
    *t;    // 这一行     *t2;    //      **t;    // 注意*t 和 **t这两个的差别
    return 0;
}

运行结果:

c++ *运算符重载

t是指向test对象的指针,(*t) 也就是一个test对象。

所以只有 *t才真正的调用了 运算符的重载函数。