C++primer 5th Edition Study notes (i)

Source: Internet
Author: User

C++primer 5th Edition Study notes (i)

The first to second chapter of the heavy and difficult content

This article mainly recorded my study C++primer (5th edition, Chinese version) encountered the heavy difficulties and analysis. Since the first to second chapter is relatively simple, here are some of the problems I have encountered in merging these two chapters.

The first chapter begins

This chapter, before the first part, is a HelloWorld-like chapter that contains basic functions, IO streams, and the introduction of classes.

Knowledge Point 1:p19,1.5, file redirection

You can execute the program in CMD under Windows or in the terminal window of the Mac,linux system and have it read into the data from one file, and then output the standard output to another file.

if We compiled the program for A.exe, the program needs to enter two integers, the integer with a comma interval, and then need to detect EOF (End_of_file, file terminator) program will stop. At this time for file redirection, we can create a new text document (B.txt), and then we record "in the document" and then save it, the system will be saved after the end of this file automatically added file terminator. Then create a new blank text document (C.txt) to make the a.exe,b.txt,c.txt in the same directory. At this point, open the terminal, go to this directory, enter a <b.txt>c.txt return confirmation. Then we implemented the file redirection.

However, cerr data that is not buffered cannot be redirected to the specified file.

(Cross-Reference: P23, chapter I) the contents of Cerr and clog will not be output immediately, and will be stored in the buffer, so you can specify (redirect) the location of their output before the buffers are refreshed. And the cerr is directly output, so only standard output can not be redirected. Read CIN, the program terminates unexpectedly, using Endl will refresh the buffer.

Section I: C + + Fundamentals

Chapter II variables and basic types

C++primer consists of four parts, here is the first part of the comparative basis, this part of a total of six chapters. It is mainly about variables, types of variables, the various situations and rules that define variables, and the rules that convert variables to each other.

Knowledge Point 2:p30,2.1.1,c++ Language concerning the provisions of the type

The basic type of the C + + language is closely related to the hardware, so many types of memory size is only given a scope, in fact, the implementation of the IDE (Llvm,gcc,visaul C + +) is in the scope, the specific implementation details are uncertain.

where bool minimum size is undefined, char minimum size is 8 bits, the minimum size of wchar_t and char16_t is 16 bits, the minimum size of char32_t is 32 bits, and the minimum size of int is 16 bits, long and long The minimum size of long is 32-bit and 64-bit, the performance of floating-point data is calculated according to the precision, where the minimum size of float is 6 digits after the decimal point, double and long double the minimum dimensional precision is 10 digits after the decimal point (the actual may be more than this precision) )。 int must not be less than short,long not less than int,long long must not be less than long. Float,double,long Double should also be a relationship of increasing precision (or the same).

Knowledge Point 3:p32,2.1.2, type conversion

The type conversion operation that the program automates takes place in the program where the IDE expects us to use type a but actually when we use Type B, b Types of objects are automatically converted to type a, and if they cannot be converted, the program will give an error.

Let's look at the automatic conversion that occurs in the expression in the assignment Operation A=b, the left side of the equals sign is called the Lvalue, B is called the right value, and the program expects the thing to be exactly the same as the right and left value types you given. If not, a forced type conversion will occur, that is, the type of B is converted to type A. If you assign a number beyond the expression range of an lvalue type to an lvalue, the left value is an unsigned type, such as unsigned char c=-1, at which point-1 (integer, negative) is converted to an unsigned character, the initial value represents the total number of values for the unsigned type, and the remainder is obtained, The remainder is the number of conversions.

Because C + + does not explicitly specify how the number of signed types should be represented, the result of this behavior is not necessarily if you assign a number that is beyond the expression range of an lvalue type to an lvalue and the left value is a signed type. We call this uncertainty the result behavior called undefined behavior.

Knowledge point 4:p36,2.1.3, escape sequence

The escape sequence for a character can be followed by a maximum of 3 8 digits, or \x followed by a number of 16 digits, so when we write "cout<<" \01234 "<<endl; " What happens is that, first, the 12 in the three-bit (012) next to \ is not treated as a 8-digit number, and will be treated as a decimal 12, and whatever is left will be treated as a normal character. This is because 012 is the number behind the \, so it is treated as a 8 binary number 12, and the corresponding 10 ASCII code table is a newline character. So the output is \n34. "\012,34" and "\12,34" are equivalent. Of course, numbers beyond the range of characters cannot be escaped.

          reference and pointers to knowledge points 5:p55,2.4.1,const

         Const is the identifier used to declare a constant, and means that we cannot change the value of a variable by using the variable name declared after const . If there is a precondition const int i=32;, then the statement assigned to I is wrong. But in int a=0;const int &p=a; , the statement that assigns a value to a is correct, because a is not declared by the Const modifier, which simply means that we cannot change the value of P's entity--a by the name P. Thus, we allow the reference to be bound to a const object, but must be const int &i=ci; This form, where CI can or may not be a const object, but the value of I after this binding operation is immutable.

Access to a constant can also be implemented with a reference to a constant or a pointer to a constant, except that a reference to a constant is an alias to the constant and cannot be assigned, but a pointer to a constant is itself an object whose value can be changed, but the value of the object pointed to cannot be changed.

If you do not want the object pointed to by the pointer to be changed, you can use the const pointer,int a=1;int *const p=a; The value of the pointer itself cannot be changed, but the value of the object it points to can be changed. such as the const int *CONST p=a; This statement causes the pointer itself and the object value it points to not be changed.

knowledge Point 6:p57,2.4.3, top-level const

The top-level const is for the const, "top" can be used to modify the const state of the adjective. A const fixed the value of the object itself, which is called the top-level const, and a const is the object that the object points to or refers to as a fixed value, and this const is called the underlying const. The top and bottom const are closely related to the copy, and two objects with the same underlying const qualification are able to copy each other, and the top-level const declares the variable and does not allow the const value to be changed again.

like int v1=9;const int *p=&v1;int *p2=p; If this statement can be compiled, then we can use the nature of the P2 to change the value of the constant pointed to by the P1, but the value of the constant can not be changed, so this disguised change in the value of the constant expression is wrong. You can judge the correctness of a statement by analyzing the const level to see if the constants in the expression have been changed.

At the end of the story, the top floor is a constraint on copy control. The general rule is "You cannot change the value of a constant". Therefore "the objects that are copied and copied should have the same underlying const qualification, or two object data types must be able to convert", for example, an int *p1,const int *P2; P1 no underlying CONST,P2 has the underlying const. p1=p2; At this point the Const int* cannot be converted to int * (if the conversion, it violates the "value of the constant can not change the constraint condition"), so p1=p2; Not legal. P2=p1; int * Can be converted to a const int *, so p2=p1 is legal.

Knowledge Point 7:p58,2.4.4,constexpr

before we get to know constexpr, we should understand the constant expression first. The so-called constant is a fixed amount, then the constant expression is a fixed value of the expression, where the "value is fixed", refers to the program compilation stage, the value of the constant expression can be determined after it cannot be modified in any way. So this fix is fixed after compiling. Like cout<<1234<<endl; In 1234, which is a constant expression, it is obvious that the literal value is a constant expression.

one of the functions of constexpr is to help programmers see if an assignment statement is a constant expression at the IDE's prompt. The method used is included in the declaration statement, such as constexpr variable type variable name = right value, if the right value is a constant, this statement is correct. The address of a global variable declared outside the body of all functions conforms to the "can be determined during compilation, the compiled value is not changed" conditions, and therefore also belongs to the constant.

In addition, a pointer declared with CONSTEXPR (for example,constexpr int *p=&v1;    In the *p, equivalent to int *const p=&v1; ) are top-level const, which means that the pointer itself is fixed.

Knowledge points 8:p61,2.5.2,auto and Decltype type declarations/indicators

The auto variable calculates the type of the right value by initializing the statement, and deduces the type of the lvalue. In this process, auto will ignore the top-level const and reference types, and can use the const auto &a=i; This approach explicitly indicates that the result to be deduced is either a top-level pointer property or a reference property. When auto deduces multiple values, the types of these values must be the same.

Decltype does not calculate the type by calculating only the value that the variable should have, the value that the expression itself should have, and the return value of the function. For variable types, Decltype retains the top-level const and referenced properties.   For an expression, the dereference expression (for example:int i=1;    int *p=&i; Decltype (*p) a=i; *p, p dereference is int & type) and parenthesized expression (e.g.decltype ((a+1)) c=i; ) will be the reference type.

C++primer 5th Edition Study notes (i)

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.