20. C ++-& quot; & amp; & quot;, & quot; | & quot; Defects of logic overload operators, & quot;, & quot; analysis of comma overload operators, Objective C

Source: Internet
Author: User

20. C ++-"&", "|" defects of logic overload operators, "," Analysis of comma overload operators, limit C

"&", "|" Logical overload operator Defects

Everyone knows"&", "|"Yes"Short Circuit" Function

  • For exampleA = (0 & B): Because the first operand0So it does not determine the content of B and directly executesA = 0
  • For exampleA = (-100 | B): Because-100Not 0So it does not determine the content of B and directly executesA = 1

You can refer to the following code:

int func(int i){    cout << "i = " << i  << endl;    return i ;}int main(){    int a= (func(0)||func(100));    cout<<"a = " << a <<endl;    return 0;}

Run print:

i=0a=0

Not calledFunc (100)Functions, also "|" logical operators have"Short Circuit" Function

 

Under the overload operator,"&", "|"It will not have the "Short Circuit" Function

See the following example:

Class Test {int mValue; public: Test (int v) {mValue = v ;}int value () const {return mValue ;}}; bool operator & (const Test & l, const Test & r) // & reload operator {return l. value () & r. value () ;}test func (Test t) {cout <"value () =" <t. value () <endl; return t;} int main () {Test t0 (0); Test t1 (1); int B = (func (t0) & amp; func (t1); cout <"B =" <B <endl ;}

Print:

Value () = 1 // enter func (t1) value () = 0 // enter func (t0) B = 0

The results show that func (t0) and func (t1) are called, and the call order isFrom right to left.

This is because when you execute func (t0) & func (t1:

The compiler is actually executed.Operator & (func (t0), func (t1 ))Function, so you need to enter func ()Initialize Two Parameters

 

The following code finds that the parameter initialization sequence is from right to left:

int print(int t){         cout<<t<<endl;         return t;}void func(int a,int b,int c){  }int main(){   func(print(1),print(2),print(3));     return 0;}

Run print:

 

","CommaHeavy LoadOperatorAnalysis

First, let's review the comma-(,) operator that comes with the compiler.

  • N-1 subexpression before comma expressionNo return value
  • Comma expressionFrom left to rightComputing, andFinal valueEqualLastThe value of an expression.

For example:

Int I = 5, B = 4; int a = (I ++, I ++, B + 4, B = 5, I ++); // B = 5, and a = (I ++) = 7. After this row is run, I is equal to 8 (I, B, a) = 10; // a = 10, and I and B remain unchanged.
  • Comma expression, through() ParenthesesTo indicate

For example:

Int a [3] [3] = {(1, 2, 3), (4, 5, 6), (7, 8, 9 )}; // only a [0] [0] = 3 is initialized, a [0] [1] = 6, a [0] [2] = 9

 

In the overload operator, commas (,) Will not be used to calculate data from left to right.

Notes for heavy-duty comma

  • Try to useGlobal functions to overload
  • The comma-based overload function must have one of the following parameters:Class(Let the compiler know this, and the comma is reloaded by the user)
  • Return Value Type of the comma-based overload FunctionMust be a reference(Because it is possible to calculate the return value)
  • The Return Value of the comma-based overload function must beValue of the last parameter("," Comma operator features)

See the following example

# Include <iostream >#include <string> using namespace std; class Test {int mValue; public: Test (int I) {mValue = I;} int value () {return mValue;} Test operator + (int I) // overload + comma operator {Test ret = this-> mValue + I; cout <ret. mValue <endl; return ret ;}; Test & operator, (const Test & a, const Test & B) // overload, comma operator {return const_cast <Test &> (B) ;}int main () {Test t1 (0); Test t2 (5); Test t3 = (t1 + 1, t2 + 1); return 0 ;}

Run print:

6 // run t2 + 11 from the rightmost. // finally run t1 + 1.

Similar to the previously analyzed "&", "|" logical overload operator defects:

The compiler is actually executed.Operator, (t1 + 1, t2 + 2)Overload operator functions.

Because the initialization parameters are initialized from right to leftThe execution order is reversed.,Run t2 + 2 first.

 

Summary:

In fact, using the comma operator "," that comes with the compiler can also be used on objects, because","It is mainly used to isolate code running and return the value of the last parameter.Not involved in object operations.

So in future development,Do not overload "," comma Operator

 

Related Article

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.