C + + Note 15: C + + extension--Three mesh operator enhancements

Source: Internet
Author: User

The behavior of the trinocular operator in the C compiler:

int main ()

{

int a=10;

int b=20;

The trinocular operator is an expression and the expression cannot be left-valued

(a<b?a:b) = 30;

printf ("a=%d,b=%d", A, b);

return 0;

}

Run the program we found that the compilation failed!

The reason we see is that the expression cannot do an lvalue .

How do you understand this sentence?

In C, the return value of an expression is a number, and the result of the expression is placed in the register of the CPU, not in memory, not in memory (note the left and right values of C and C + +).

That is to say, in C language, A=10,B=20, execute A<B?A:B statement, is "10<20?" Yes! Good! Then return the value of a! "Just return the number" 10 ", then the 10=30, and assign 30 to 10? That's what it means, it's a mistake!

In C + +, an expression returns a variable, a variable of itself.

Compile the same code in C + +:

#include <iostream>

using namespace Std;

int main ()

{

int a=10;

int b=20;

The trinocular operator is an expression and the expression cannot be left-valued

(a<b?a:b) = 30;

printf ("a=%d,b=%d", A, b);

System ("pause");

return 0;

}

Operation Result:

A=30,b=20

Please press any key to continue ...

So how do you achieve the same effect in C?

Let's think about it, if you want the expression to be an lvalue, that means the trinocular expression should return a memory space ! That's the first address of memory! What is the first address of memory? It's a pointer.

A<b? A:b This expression is finished, I don't want it to return a value of 10, if I can return the address of a, and then assign a value to that address.

I can write this: (A < b? &a: &b) = 30, through this address can indirectly modify the pointer to the memory space, also indirectly modify the value of a, note that the parentheses before the asterisk "*" is to modify the corresponding value. We modify and then run the discovery is the same as the C + + run.

int main ()

{

int a=10;

int b=20;

The trinocular operator is an expression and the expression cannot be left-valued

* (a<b?&a:&b) = 30;

printf ("a=%d,b=%d", A, b);

return 0;

}

Operation Result:

A=30,b=20

Press any key to continue

So, C + + compiler is to help our programmers to complete the work of fetching addresses.

Conclusion :

1, the C language returns the value of the variable, the C + + language returns the variable itself;

The three-mesh operator in the C language returns the value of the variable and cannot be used as a left-hand value;

The three-mesh operator in C + + can return directly to the variable itself, so it can appear anywhere in the program.

2, note: The three mesh operator may return a value if one is a constant value, it cannot be used as an lvalue, as

(A < b?) 1:B) = 30;

3. How does the C language support features like C + +?

When the condition of the Lvalue: to have memory space, the C + + compiler helps the programmer to take an address.

Long Press to unlock

Unlock more Insider Stories

Legal programming

: Lightspeed-tech

Technology-driven Life

C + + Note 15: C + + extension--Three mesh operator enhancements

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.