C++重载双目运算符(1)(对象与对象之间)

重载双目运算符需要涉及到两个对象,则在写operator构造函数的时候要注意参数的传递

#include <iostream>
using namespace std;
class test
{
public:
    test() {}
    test(int a1) :a(a1) {}
    test operator + (const  test&item)
    {
        a = a + item.a;
        return test(a);
    }
    void show() {
        cout << a << endl;
    }
private:
    int a;
};

int main()
{
    test t1(1);
    test t2(3);
    test t;
    t = t1 + t2;
    t.show();
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/god-for-speed/p/10842995.html