C ++ primer plus Reading Notes (Chapter 2 and Chapter 5)

Source: Internet
Author: User

Chapter 4 composite type

The most influential composite type is class, which is the bastion of OOP that will be learned. Composite types are certainly used to process more complex data types.

 

C ++ does not detect array out-of-bounds

Code out of bounds may run and end correctly, but it may also cause the entire program or even the operating system to crash. The arrays of C ++ are actually closely related to pointers, which may be different in form and essentially have no difference. That is, a [7] And * (a + 7) are equivalent to the compiler.

 

String constant

A String constant can be defined in C ++, but if a character pointer is initialized using a string pointer, this pointer cannot be used to change this constant. If two identical constants are defined, different systems may adopt different policies to determine whether to retain only one or more copies.

 

Several Functions

Getline () accepts a row and discards the line feed. Get () accepts a row but does not discard the carriage return, and keeps the carriage return in the input buffer. If you keep using get () to read a row, this carriage return is not processed. You need to use a get () overload function to read the last carriage return.

 

Anonymous structure/shared body

The so-called anonymity is to use it all at once instead of specifying the type name when defining a struct or a shared body. When defining a struct, you can directly use an anonymous shared body as a member variable of the struct without declaring the shared body in the structure.

 

Pointer and variable

A variable regards a value as a given amount, and an address as a derived amount. A pointer regards an address as a given amount and a value as a derived amount. I prefer "*", which is called the unreference operator.

Before removing the reference (*) from the pointer application, initialize the pointer to a definite and appropriate address.

 

Arithmetic Operations on pointers

The addition of 1 to the arithmetic operation of the pointer is the memory size units occupied by the basic type. Int A [5] where the basic type of A is int *, so the + 1 address is added with 4, and the basic type of & A is int (*) [5], & A + 1 address plus 20.

 

C ++ Memory Management Method

C ++ has three data memory management methods: Automatic Storage, static storage, and dynamic storage.

Automatic Storage:

Regular variables defined in a function use an automatic bucket, which is called an automatic variable. When a function is called, it disappears. Is a stack storage structure.

 

Static storage:

There are two ways for a variable to become a static variable: one is to define it outside the function, and the other is to use the keyword static when declaring the variable.

 

Dynamic Storage:

The new and delete operators provide a more flexible method than automatic and static variables. They manage a memory pool, which is called a free storage space or heap in C ++.

 

Array substitution:

Vector and array can be used to replace arrays, And the. At () member variables are used to detect whether the array is out of bounds.

 

 

 

 

Chapter 5 loop and relational expressions

Auto-increment/auto-increment operators & side effects and sequence points:

A side effect is that something (such as the value stored in the variable) is modified during expression calculation. The semicolon in the statement in C ++ is a sequence point, and the end of a complete expression is also a sequence point. A completed expression refers to a subexpression that is not a larger expression. C ++ only ensures that all modifications to the value assignment operator, increment operator, and decrease operator are completed when an ordered point is reached.

Y = (4 + x ++) + (6 + x ++ );

Expression 4 + x ++ is not a complete expression. Therefore, C ++ does not guarantee that the value of X increases by 1 immediately after the subexpression 4 + x ++ is computed. In this example, the entire value assignment statement is a complete expression, and the semicolon indicates the order point. Therefore, C ++ only ensures that the program is executed before the next statement, the value of X is incremented twice. C ++ does not specify whether to increase the X value after calculating Each subexpression or the X value after the entire expression is calculated. Therefore, we should avoid using such expressions.

When a system variable is not used, the auto-increment and auto-increment effects of the smart compiler are the same. However, when we use our own classes, the prefix operator should be better, because the latter should open up a temporary space.

 

Loop Boundary

The boundary of the loop must be well handled, especially in some "good" loops (after reaching the boundary, it still does not exit because it is illegal ), therefore, we need to choose hard rules or the Sentry positions.

 

For, while, do... while

The for loop is mainly used to process the loops with known times, and the latter two are used to process the loops with unknown loops. The first two are also called the entry condition loop, and the latter is also called the exit condition loop.

 

Type alias:

C ++ has two types of Alias definition: # define and typedef. The latter is recommended because define may have some problems in the following situations.

# Define int * pword

Pword P1, P2;

Our intention may be to define two pointers to the int type, but P2 is indeed an int type. Typedef can avoid such problems.

 

Array traversal:

C ++ 11 adds two types of array traversal.

Int A [5] = {1, 2, 3, 4, 5 };

For (int x: a) {...} to access each element through X, if you want to change each element

For (Int & X: a) {...} uses the reference format on the left.

 

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.