C + + Primer Fifth Edition: Chapter 2nd

Source: Internet
Author: User

C + + Primer Fifth Edition 2nd Chapter study Notes * * * *

Experimental code is debugged in red Hat 6.6 or vs 2013 * * * * *

The content of the article is based on the current knowledge writing, the limitations of cognition * * *

Today to learn C + + Primer 2nd Chapter, has not read, first write a little reading experience and notes.

The object type determines the data that the object contains and the operations that can participate, and also determines the size of the storage space.
Do not use char in arithmetic expressions, because different machine implementations differ, resulting in different results.
Single-precision calculations are not necessarily faster than double precision.

When a C + + type does not match, it is an automatic type conversion.

There are four ways to initialize C + + variables:

1.int a=0;

2.int a={0};

3.int a{0};

4.int A (0);

The way to initialize a variable with curly braces is called: List initialization.

Use list initialization for built-in types to initialize a value, and 2 types are not available, there is a risk of loss of information, list initialization will not be completed, for example:

a long double a=3.1415926;

int B{a}; Compile error, unable to complete.

If you do not specify an initial value when defining a variable, the variable is initialized by default (default initialized), at which time the variable is given the "defaults". The default value is exactly what is determined by the variable type, and the position of the variable is also affected.

If a variable of a built-in type is not explicitly initialized, its value is determined by the location defined. Variables defined outside of any function body are initialized to 0. One exception, however, is that the built-in type variables defined inside the function body will not be initialized (uninitialized). The value of an uninitialized built-in type variable is undefined and an error is raised if an attempt is made to copy or otherwise access such a value.

Each class determines how it initializes the object. Also, it is up to the class to decide whether to allow the definition of an object without initialization. If the class allows this behavior, it will determine what the initial value of the object is.

This paragraph of the above is a lot of content, learn C language friends should be able to know, this paragraph and the scope of the variable is very much related:

Where a static variable is initialized only once, and is not explicitly initialized, the default is initialized to 0, which is why "variables defined outside of any function body are initialized to 0". Ps: static variables and external variables are ready when the program is into memory. in addition, C + + is different from C, if the built-in type of local variables are not initialized, is not accessible in any way, including output and replication, VS2013 compile Error! Finally, the lesson from the textbook is: Do not use uninitialized objects! C + + is a static type (statically typed) language. When a reference is defined, the program binds the reference to its initial value (BIND) instead of copying the initial value to the reference. Once the initialization is complete, the reference is bound together with its initial value object. Because the reference cannot be re-bound to another object, the reference must be initialized. Because the reference itself is not an object, you cannot define a reference reference. What do you mean by this sentence? As an example: int c; int &b=c; int &a=b ;   //a成为另一个c的引用. int &&d=b;  //这里即体现了引用(&d)的引用(&&d),

Pointer value

The value of the pointer (that is, the address) should be one of the following 4 states:

1. Point to an object.

2.  Points to the next position in space occupied by the object.   This I do not understand what meaning, search for a bit, found that there is an article explanation is int *p = &i; p++;

3. A null pointer means that the pointer does not point to any object.

4. Invalid pointer, which is a value other than the above case.

In addition, pointers that are initialized are extremely dangerous.

As long as the pointer has a valid value, it can be used in a conditional expression. And the use of arithmetic values as a condition (parameter
See 2.1.2, P. 35th) Follow the same rules, if the value of the pointer is 0, the condition takes false:
int ival = 1024;
int *pi = 0; Pi Legal, is a null pointer
int *pi2 = &ival; PI2 is a legitimate pointer to the Ival address.
The IF (PI)//PI value is 0, so the value of the condition is false
if (PI2)//Pi2 points to ival, so its value is not 0, the value of the condition is true

Here the If (pi) pi value is 0, so the value of the condition is false, at first think this is wrong usage, pi is a pointer, its value is an address, it is impossible to do bool judgment, later in the group for advice, a prawn gave guidance, in fact, if (PI) is with if Pi = NUL L) equivalent, think about it: D, yes, the two are equivalent, so the result of the judgment is false. Finally, the first chapter of a while loop of knowledge points, in the first chapter of the exercises, I saw such a piece of code
while (a>b) if (m>n) ...; else ...;

The above code while the loop is followed by If,else, and while the loop body does not have curly braces, then the while loop body is where to go, I did not understand, always thought to the first semicolon cutoff, until see this code, only know If...else ... This code is considered as a whole, so the above is a while loop.

Write here for the time being, to be continued.

Continue: Cannot define a pointer to a reference

C + + Primer Fifth Edition: Chapter 2nd

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.