注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。
测试环境:Ubuntu 10.10
GCC版本:9.2.0
一、潜规则
1) 逻辑运算符的原生语义
- 操作数只有两种值(true和false)
- 逻辑表达式不用完全计算就能确定最终值(重点:短路法则)
- 最终结果只能是true或者false
编程实验
逻辑表达式
38-1.cpp
#include <iostream>
#include <string>
using namespace std;
int func(int i)
{
cout << "int func(int i) : i = " << i << endl;
return i;
}
int main()
{
//逻辑与
if( func(0) && func(1) ) //func(1)没有执行
{
cout << "Result is true!" << endl;
}
else
{
cout << "Result is false!" << endl;
}
cout << endl;
//逻辑或
if( func(0) || func(1) ) //如果if( func(1) || func(0) )func(0)不执行
{
cout << "Result is true!" << endl;
}
else
{
cout << "Result is false!" << endl;
}
return 0;
}
操作:
1) g++ 38-1.cpp -o 38-1.out编译正确,打印结果:
int func(int i) : i = 0
Result is false!
int func(int i) : i = 0
int func(int i) : i = 1
Result is true!
实例目的:为了回顾逻辑操作符短路原则(判断顺序从左到右)。
二、重载逻辑操作符
逻辑操作符可以重载吗?重载逻辑操作符有什么意义?
编程实验
重载逻辑操作符
38-2.cpp
#include <iostream>
#include <string>
using namespace std;
class Test
{
int mValue;
public:
Test(int v)
{
mValue = v;
}
int value() const //const修饰的对象只能调用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(operator&&(func(t0),func(t1))) //函数调用形式
{ //func(t0)和func(t1)的值,且这两个的计算顺序不确定
cout << "Result is true!" << endl;
}
else
{
cout << "Result is false!" << endl;
}
cout << endl;
if(operator||(func(1), func(0))) //函数调用形式
{
cout << "Result is true!" << endl;
}
else
{
cout << "Result is false!" << endl;
}
return 0;
}
操作:
1) g++ 38-2.cpp -o 38-2.out编译正确,打印结果:
Test func(Test i): i=1 函数都被调用,不符合原生语义的逻辑与&&(短路法则失效)
Test func(Test i): i=0
the result is false
Test func(Test i): i=0 函数都调用,不符合原生语义的逻辑或||(短路法则失效)
Test func(Test i): i=1
the result is true
分析:
逻辑与(&&)的两个函数调用顺序反了,不符合原生顺序(左至右)。逻辑或也不是从左至右顺序!
1)问题的本质分析
1.C++通过函数调用扩展操作符的功能
2.进入函数体前必须完成所有参数的计算
3.函数参数的计算次序是不定的
4.短路法则完全失效
逻辑操作符重载后无法完全实现原生的语义
2)一些有用的建议
- 实际工程开发中避免重载逻辑操作符
- 通过重载比较操作符代替逻辑操作符重载(对象与true或false比较)
- 直接使用成员函数代替逻辑操作符重载
- 使用全局函数对逻辑操作符进行重载
小结
1)C++从语法上支持逻辑操作符重载
2)重载后的逻辑操作符不满足短路法则(陷阱)
3)工程开发中不要重载逻辑操作符
4)通过重载比较操作符替换逻辑操作符重载
5)通过专用成员函数替换逻辑操作符重载