C + + Primer Fourth Edition reading notes (iv) expressions

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators

An expression is a combination of one or more operands through an operator. The simplest expression contains only one literal constant or variable. More complex expressions are composed of operators and one or more operands.

Each expression produces a result. If there is no operator in an expression, the result is the value of the operand itself. When an object is used where its value is needed, the value of the object is evaluated.

The meaning of the operator-what the operator does and the type of the result of the operation-depends on the type of the operand.

C + + provides a unary operator and a two-tuple operator for both operators. An operator that acts on an operand is called a unary operator. such as the address character (&) and the reference operator (*), while the two-dollar operator acts on two operands, such as the addition operator (+) and the subtraction operator (-).

First, arithmetic operators

When there is only one operand negative, the symbol for the result value of the modulo operation can be determined by the symbol of the numerator (dividend) or denominator (divisor). If the result of the modulo is the symbol of the molecule, the value to be removed is to be rounded on the zero side, and if the modulo matches the symbol of the denominator, the value to be removed is rounded to the negative infinity side.

Ii. relational operators and logical operators

Relational operators and logical operators
Operator Function
! Logical Non-
<<=>>= Less than
Less than or equal
Greater than
Greater than or equal
==!= Equals
Range
&& Logic and
|| Logical OR

2.1 Logic and, logic, or operators

The result is true only if the two operands of the logical and (&&) operator are true.

For logical OR (| | ) operator, as long as one of the two operands is true, its value is true.

Note: The logical and logical OR operator always calculates the operand first and then calculates its right-hand operand. The right operand is solved only if the value of the left-hand operand cannot determine the result of the logical expression. We often call this evaluation strategy "short-circuit evaluation".

2.2 should not be threaded using relational operators

Third, bitwise operators

The bitwise operator uses the operand of an integral type. The bitwise operator treats its integer operand as a collection of bits, providing the ability to inspect and set for each bit.

Bitwise operators
Operator Function
~ Bit negation
<<
>>
Move left
Move right
& Bit and
^ Bit XOR or
| Bit or

The type of an integer manipulated by a bitwise operator can be either signed or unsigned. If the operand is a negative number, how the bit operator handles the sign bit of its operand depends on the machine. So their application may be different: programs implemented in one application environment may not be available for another application environment.

For bitwise operators, it is strongly recommended to use the unsigned integer operand because the system does not ensure how to handle the sign bit of its operand.

The left shift operator (<<) inserts 0 on the right to complement the empty space. For the right-shift operator (>>), if its operand is an unsigned bit, 0 is inserted from the left, and if the operand is a signed bit, a copy of the caret bit or 0 value is chosen, depending on the implementation. The right operand of a shift operation cannot be a negative number, and must be a value that is strictly less than the left-hand operand. Otherwise, the effect of the action is undefined.

3.1 Use of Bitset objects or integer values

3.2 Using the shift operator for IO

Iv. Assignment operators

The left operand of the copy operand must be a non-const left value.

V. Self-increment and decrement operators

VI. sizeof operator

The function of the sizeof operator is to return the length of an object or type name, the type of the return value is size_t, and the length of the unit is bytes. The result of a sizeof expression is a compile-time constant, which has the following three syntactic forms:

sizeof (type name);

sizeof (expr);

sizeof expr;

Applying sizeof to the expression expr will get the type length of the result of the expression.

When sizeof is used for expr, the value of the expression expr is not evaluated. Especially in sizeof *p, the pointer p can hold an invalid address because the p does not need to be dereferenced.

The results of using sizeof partially depend on the type involved:

1, for the char type or value is a char type of expression to do sizeof operation guaranteed 1;

2. A sizeof operation on a reference type returns the size of the memory space required to hold the object of this reference type.

3, the pointer does the sizeof operation will return the size of the memory required to hold the pointer; Note that if you want to get the size of the object that the pointer points to, you must dereference the pointer.

4, the array to do the sizeof operation is equivalent to the number of its element type to do the sizeof operation result multiplied by the count of elements.

Seven, new, and delete expressions

When you define a variable, you must specify its data type and name. When you create an object dynamically, you only need to specify its data type, rather than naming the object. Instead, the new expression returns a pointer to the newly created object, which we use to access the object.

7.1 Initialization of dynamically created objects

Dynamically created objects can be initialized by initializing variables:

int I (1024);

int *pi = new int (1024);

string s (10, ' 9 ');

String *ps = new String (10. ' 9 ');

C + + initializes dynamically created objects using the direct initialization syntax rules. If an initial value is provided, the new expression is allocated to the required memory, and the memory space is initialized with the given initial value.

7.2 Default initialization for dynamically created objects

If display initialization is not provided, the dynamically created object is the same as the variable that is defined within the function. For objects of class type, initialized with the default constructor of the class, and objects with built-in types are not initialized.

7.3 Running out of memory

If the new expression fails to acquire the required memory space, the system throws an exception named Bad_alloc.

7.4 Undoing dynamically created objects

When a dynamically created object is exhausted, the programmer must display the memory occupied by the object back to the free storage area. C + + provides a delete expression that releases the address space pointed to by the pointer.

7.5 deletion of 0 value pointers

If the value of the pointer is 0, then the delete operation on it is legal, but it does not make any sense:

int *ip = 0;

Delete IP;

C + + Guarantee: The pointer to delete the 0 value is safe.

7.6 After the delete, reset the value of the pointer

When the pointer is removed, the pointer becomes a dangling pointer. The dangling pointer points to the memory where the object was stored, but the object no longer exists. Dangling pointers often cause program errors and are difficult to detect.

Once you have deleted the object pointed to by the pointer, set the pointer to 0 immediately, which makes it very clear that the pointer is no longer pointing to any object.

Dynamic allocation and recycling of 7.7 const objects

const INT *PCI = new const int (1024);

Like other constants, dynamic creation of const objects must be initialized at creation time, and their values cannot be modified once initialized.

Warning: Dynamic memory management error-prone

The following three common program errors are related to dynamic memory allocation:

(1), deleting (delete) pointer failed to allocate memory dynamically, so the block memory cannot be returned to the free storage area. The failure to delete the dynamically allocated memory is called a memory leak (leak). Memory leaks are difficult to find, and it is generally necessary to run out of memory space after the application has been running for a period of time before the memory leak is revealed.

(2), read and write the deleted object. If you delete the object pointed to by the pointer and set the pointer to a value of 0, it is easier to detect such errors.

(3), use the delete expression two times for the same memory space. When two pointers point to the same dynamically created object, an error occurs when it is deleted. If a delete operation is made on one of the pointers, the memory space of the object is returned to the non-free store, then the second pointer is deleted, and the free storage area may be destroyed.

7.8 Deleting a const object

Delete PCI;

Even if the operand of the delete expression is a pointer to a const object of type int, the statement also effectively reclaims what PCI points to.

Eight, type conversion

The validity of an expression depends on the type of operand, and the meaning of a legitimate expression is determined by the type of operand.

If the two types can be converted to each other, the two types are said to be related.

Instead of adding two different types of values directly together, C + + provides a set of translation rules to convert two operands to the same data type before performing arithmetic operations. These conversion rules are automatically executed by the compiler without the need for programmer intervention. Therefore, they are also known as implicit type conversions.

8.1 When an implicit type conversion occurs

The compiler applies type conversion rules to objects of built-in types and class types, if necessary. Implicit type conversions occur in the following situations:

1. In a mixed-type expression, the operands are converted to the same type:

int ival;

Double Dval;

Ival >= Dval;//ival converted to double

2. The expression used as a condition is converted to type bool:

3. Initializes a variable with an expression, or assigns an expression to a variable, the expression is converted to the type of the variable

8.2 Arithmetic Conversions

8.2.1 conversion between signed and unsigned types

If an unsigned value is used in an expression, the defined conversion rule will protect the precision of the operand. The conversion of the unsigned operand depends on the relative size of the integral type in the machine, so such conversions are inherently dependent on the machine.

8.3 Other implicit conversions

8.3.1 Pointer Conversion

When working with arrays, most of the time the array is automatically converted to a pointer to the first element:

int ia[10];

int *IP = IA;

Exceptions that do not convert an array to pointers are when the array is used as the operand of the Fetch address (&) operator or the operand of the sizeof operator, or when initialized with a reference to an array of arrays, the array is not converted to a pointer.

C + + also provides two additional pointer conversions: pointers to arbitrary data types can be converted to void* types, and integer value constants 0 can be converted to any pointer type.

8.3.2 Conversion to BOOL type

8.3.3 the conversion of the arithmetic type to the bool type

8.3.4 conversions and enumeration types

C + + automatically converts an object or enumeration member of an enumerated type to an integral type, and its conversion results can be used wherever an integer value is required.

Nine, display conversion

The display transformation is called forced type conversion (CAST), which includes the following column name-enforced type conversion operators:

Static_cast, dynamic_cast, Const_cast and reinterpret_cast.

9.1 When to force type conversions

Because you want to override the usual standard conversions, you need to show the use of coercion type conversions.

Another reason for displaying the use of forced type conversions is that there may be multiple transformations that require a specific type conversion to be selected.

9.2 Named coercion type conversions


C + + Primer Fourth Edition reading notes (iv) expressions

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.