逻辑运算符(&&
,||
,!
)的原生语义
true
和 false
)true
或者 false
示例1:逻辑表达式
Demo
#include <iostream>
#include <string>
using namespace std;
int func(int i)
{
cout << "int func(int i) : i = " << i << endl;
return i;
}
int main()
{
if( 0 && 1 ) {
cout << "Result is true!" << endl;
}
else {
cout << "Result is false!" << endl;
}
cout << endl;
if( 0 || 1 ) {
cout << "Result is true!" << endl;
}
else {
cout << "Result is false!" << endl;
}
cout << endl;
if( func(0) && func(1) ) {
cout << "Result is true!" << endl;
}
else {
cout << "Result is false!" << endl;
}
cout << endl;
if( func(0) || func(1) ) {
cout << "Result is true!" << endl;
}
else {
cout << "Result is false!" << endl;
}
return 0;
}
编译运行
Result is false!
Result is true!
int func(int i) : i = 0
Result is false!
int func(int i) : i = 0
int func(int i) : i = 1
Result is true!
【问题】逻辑操作符可以重载么?重载逻辑操作符有什么意义?
示例2:重载逻辑操作符
Demo
#include <iostream>
#include <string>
using namespace std;
class Test
{
int mValue;
public:
Test(int v)
{
mValue = v;
}
int value() const
{
return mValue;
}
};
// 尝试利用全局函数进行重载&&
bool operator && (const Test& l, const Test& r)
{
return l.value() && r.value();
}
// 尝试利用全局函数进行重载||
bool operator || (const Test& l, const Test& r)
{
return l.value() || r.value();
}
Test func(Test i)
{
cout << "Test func(Test i) : i.value() = " << i.value() << endl;
return i;
}
int main()
{
Test t0(0);
Test t1(1);
if( func(t0) && func(t1) ) // <=>函数调用形式: if(operator && (func(to),func(t1)))
{
cout << "Result is true!" << endl;
}
else
{
cout << "Result is false!" << endl;
}
cout << endl;
if( func(1) || func(0) ) // <=>函数调用形式: if(operator || (func(to),func(t1)))
{
cout << "Result is true!" << endl;
}
else
{
cout << "Result is false!" << endl;
}
return 0;
}
编译运行
Test func(Test t) : t.value = 1
Test func(Test t) : t.value = 0
Result is false!
Test func(Test t) : t.value = 1
Test func(Test t) : t.value = 0
Result is true!
问题:短路法则失效,且调用顺序不定
问题的本质分析
逻辑操作符重载后无法完全实现原生语义
Tips
true
或 false
比较)代替逻辑操作符重载原文:https://www.cnblogs.com/bky-hbq/p/13903871.html