#include <iostream>
using namespace std;
class Test
{
private:
int m_a;
public:
Test(int a);
bool operator &&(Test &t);
Test operator +(const Test &t);
};
Test::Test(int a)
{
m_a=a;
}
bool Test::operator &&(Test &t)
{
if(this->m_a && t.m_a)
{
return true;
}
else
{
return false;
}
}
Test Test::operator +(const Test &t)
{
cout << "operator + constructor!" << endl;
this->m_a=this->m_a+t.m_a;
return *this;
}
int main()
{
Test t1(0);
Test t2(1);
Test t3(3);
if(t1&&t2)
{
cout << "aaaaaaaaaaaaaa" << endl;
}
else
{
cout << "bbbbbbbbbbbbbb" << endl;
}
if(t1 && (t2 + t3)) //违背了短路规则,因此不要重载&&或||
{
cout << "cccccccccccccc" << endl;
}
else
{
cout << "dddddddddddddd" << endl;
}
return 0;
}
执行结果:
本来应该是直接打印ddddd,并不会执行+的重载运算,但是这里还是会运算那个+号,因此说不要重载&&和||