C + + Primer 5th Chapter 4 Learning Notes

Source: Internet
Author: User

4th Chapter Expressions

An expression is a formula of an operand and an operator, and an expression evaluates to a result, and a single variable or literal value is also an expression, and the result is itself.

Operators are: Unary operators, two-ary operators, and ternary operators. A unary is a function object, two Yuan two objects, and so on. A function call is a special operator (this operator has no effect on the number of objects limit)

Key points of an expression: precedence, binding, order of evaluation.

Lvalue and Rvalue: These two nouns are inherited from the C language, in C + +, when an object is used as an rvalue, it uses its value (content), and when the object is used as an lvalue, it uses its identity (in-memory position). Popular to understand the words, for example

int a=5;//The code declaration defines an int variable with a value of 5, which performs the action of storing an int variable A in memory with a 0x7fff0444-0x7fff0447 of 4 bytes. and stored a binary 5 (101)

When a is used as a right value, for example

int b=a;//Here A is placed on the right, using the right value of a, that is, the value of a is 5, so b=5.

When assigning a value to variable a, the shape

A=7; At this point A is placed on the left side, is used as the left value, the use is no longer its value, using its identity, even with this address 0x7fff0444-0x7fff0447.

The above int size and memory address are assumed for illustrative purposes.

The behavior of different operators in the expression and return values are not the same, some require the left operand, some require the right operand, for example, the address character &, the right operand, that is, the object on the right side of the:&a; (take the address of variable a), and then increment + + The operand is required on the left: p++. The return value is also different, some return an lvalue, and some return a right value. Which returns the Lvalue, which returns the right value, needs to be taken care of, most of the return is the right value.

The left value can be used as an lvalue or as an rvalue, but the right value is used only as an rvalue.

The operator requires not only the left or right operand, but also an lvalue or Rvalue object, such as the assignment operator "=", which is required to have an lvalue on the left, a left and right value on the right, and an lvalue when the operation is complete.

A composite expression is a combination of multiple operators that, when contained in multiple operators and operands, depends on the combination of conditions, priorities, and sequencing.

The specific content of precedence, binding law, and order of evaluation is continued in the basic mathematics, so we will not repeat it here.

An additional point: when the priority is the same, some expressions are not necessarily evaluated, and the resulting results are uncertain, so this situation must be avoided.

For example, the operation of several functions: F () +g () *f () +g (), when f () and g () are simultaneously changing a global variable, it is possible to cause an indeterminate result.

C + + Primer 5th Chapter 4 Learning Notes

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.