1. native semantics of logical operators
(1) Operands have only two values (True and False)
(2) Logical expression can determine the final value without fully calculating
(3) The final result can only be true or false
"Programming Experiment" logical expression
#include <iostream>using namespacestd;intFuncinti) {cout<<"int func (int i): i ="<< I <<Endl; returni;}intMain () {//Short Circuit of &&: Only one is 0 short//int func (int i): i = 0//Result:false if(Func (0) && func (1) ) {cout<<"result:true"<<Endl; } Else{cout<<"Result:false"<<Endl; } cout<<Endl; //|| Short circuit: As long as 1 is 1, short circuit//Output://int func (int i): i = 1//result:true if(Func (0) || Func1) ) {cout<<"result:true"<<Endl; } Else{cout<<"Result:false"<<Endl; } return 0;}
2. overloaded logic operators
(1) Problems and analysis of overloading
①c++ functions of extension operators through function calls
② The calculation of all parameters must be completed before entering the function body
The order of ③ function parameters is variable
The ④ short circuit rule completely fails
(2) The logical operator cannot fully realize the original meaning after overloading
(3) Some useful suggestions
① to avoid overloading logical operators in actual engineering development
② replacing logical operator overloading by overloading comparison operators
③ directly using member functions instead of logical operator overloading
④ using global functions to overload logical operators
The "Programming experiment" Operation logical operator
#include <iostream>using namespacestd;classtest{intMvalue; Public: Test (intV) {Mvalue =v;} intValue ()Const { returnMvalue; }};//using global function overloading && operatorsBOOL operator&& (Consttest& LP,Consttest&RP) { returnLp.value () &&rp.value ();}//Leveraging global function overloading | | OperatorBOOL operator|| (Consttest& LP,Consttest&RP) { returnLp.value () | |rp.value ();} Test func (Test i) {cout<<"int func (Test i): i.value () ="<< I.value () <<Endl; returni;}intMain () {Test T0 (0); Test T1 (1); //Output: (Analysis: Equivalent to a function call, enter the function body, the value of the parameter must be calculated)//int func (Test i): i.value () = 1//int func (Test i): i.value () = 0//Result:false//The short-circuit rule completely fails, overloading && changing the semantics of its original short-circuit if(Func (t0) && func (T1))//equivalent to function call operator&& (func (t0), func (T1)){cout<<"result:true"<<Endl; } Else{cout<<"Result:false"<<Endl; } cout<<Endl; //Output: (Analysis: Equivalent to a function call, enter the function body, the value of the parameter must be calculated)//int func (Test i): i.value () = 1//int func (Test i): i.value () = 0//result:true//the short-circuit law completely fails, overloading | | Changed the semantics of its original short circuit. if(func (t0) | | | func (t1))//equivalent to function call operator&& (func (t0), func (T1)){cout<<"result:true"<<Endl; } Else{cout<<"Result:false"<<Endl; } return 0;}
3. Summary
(1) C + + syntax support for logical operator overloading
(2) The overloaded logic operator does not meet the short circuit rule
(3) do not overload logical operators in engineering development
(4) Replacing logical operator overloads by overloading comparison operators
(5) Replacing the overload of a logical operator with a dedicated member function
38th lesson the trap of logical operators