38th lesson the trap of logical operators

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.