C++primer Chapter II Part II

Source: Internet
Author: User
Tags modifiers

//1. A declaration statement consists of a basic data type and a list of declarators immediately following it. Each declarator names a variable and specifies that the variable is a type that is related to the base data type. In the same declaration statement, the basic data types are the same, but the declarators are not necessarily the same. //base type: is a type specifier that can be decorated with a const, before the declarator in the declaration statement. The base type provides the most common data types that are used to build the declarator. //declarator: is part of the declaration, including the name and type modifiers of the defined object. where type modifiers (such as const,*,&, etc.) are optional.     intValue0 =0;//in this definition statement, int is the base data type and VALUE0 is the declarator. The result of this declaration statement is that the declarator value is specified as an int type.     int*pvalue0, value1;//in this definition statement, 2 variables of different types are defined. Pvalue0 is a int* type whose type modifier * is part of the declarator, value1 is of type int, and their base data type is int. //2. Reference: Another name is given to the object, the reference must be initialized, and the reference cannot be re-bound to another object. The reference itself is not an object. //The reference type is strictly matched to the object it is bound to, with two exceptions: 1. When initializing a constant reference, it is allowed to use any expression that can be converted to that reference type as the initial value. 2. The parent class reference can accept child class objects.     DoubleDATA0 =3.14; Const int&data1 = DATA0;//legal, but Data1 and data0 have different addresses.//3. Pointer: is a composite type that points to another type. It is an object in itself. The void* pointer is a special kind of pointer that can be used to store the address of any object and cannot be referenced by the void* pointer. //The pointer type must match the object exactly, but there are two exceptions: 1. A pointer to a constant is allowed to point to a very const object. 2. The parent pointer can point to the child class object.     intNumber =0; Const int*pnumber0 = &Number ; int*ConstPNumber1 = &number;//the symbol closest to PNumber1 is const, meaning that PNumber1 itself is a constant whose type is determined by the remainder of the declarator. //4. By default, the const object is set to valid within the file. When a const variable with the same name appears in multiple files, it is equivalent to defining these separate variables separately in different files. If a const variable is defined in the header file, then multiple source files containing the header file will define their own independent const variable. //if you want to share the const variable between files, define it in a source file, and make an extern declaration in the header file, all the source files that contain this header file will share a const variable. //Const is classified as the top-level const and the underlying const. The top-level const can indicate that any object is a constant, while the underlying const represents the object that the pointer refers to as a constant. Because the reference is not an object, the const of the reference type is the underlying const//there is a significant difference between the top-level const or the underlying const when performing object copies. Where the top-level const is unaffected. On the other hand, when the underlying const object performs a copy operation, the object required to be copied and copied must have the same underlying const qualification or the data type of two objects to be converted. //Const is a type modifier    Const int*p0 = nullptr;//The const here is the underlying const    int*ConstP1 = nullptr;//The const here is the top-level const    Const intCount =0;//The const here is the top-level const//5. Type aliases: Use the keyword typedef. typedefint*Pint; intNdata =0; ConstPint PData = &nData;//equivalent to: int *const pData = &nData; Note point: Pint is used in the declaration, its basic data type is a pointer, if written as const int *pdata = &nData; Its basic data type is int, * becomes    Part of the declarator. //pData = nullptr; //error C3892: "PData": Cannot assign a value to a constanttypedefclassB {}b; //B is the alias of B//6.auto: Let the compiler infer the type of the variable by its initial value. Obviously, the variables defined by auto must have an initial value and must have the same basic type. //Auto The inferred type is not exactly the same as its initial value in the following cases, the compiler appropriately changes the result type to make it more compliant with the initialization rule//A: The compiler refers to the type of the object as the type of auto. //B:auto ignores the top-level const and preserves the underlying const//C:auto will convert the function name to the corresponding function pointer type//D:auto will convert the array name to the corresponding pointer type//7.decltype: You want to infer the type of the variable you want to define from the expression, but you do not want to initialize the variable with the value of the expression. In this procedure, you only have to type the expression and not actually evaluate the value of the expression. //Decltype does not perform conversions that are performed by the compiler in auto. //If the expression used by Decltype is not a variable, DECLTYPE returns the type corresponding to the expression. If the evaluation result of an expression is an lvalue, decltype acts on the expression (not a variable) to get a reference type.     intNNumber0 =Ten; int*pnnumber0 = &NNumber0;                Decltype (NNUMBER0) nNumber1; //nNumber1 is of type intDecltype (PNNUMBER0) nNumber2;//nNumber2 for int* typeDecltype ((NNUMBER0)) NNumber3 = NNumber0;//nNumber3 for int& typeDecltype (*PNNUMBER0) nNumber4 = NNumber0;//nNumber4 for int& type//8. The structure and class of the class body can be followed by the definition of its object    classa {} A; //A is an object of Class A//9. When the preprocessor sees the # include tag, it replaces # include with the contents of the specified header file//You typically use #ifdef #endif to prevent duplicate inclusions. #ifdef #endif is a preprocessing specified//preprocessing variables ignore scope restrictions in the C + + language #define is a preprocessing directive that defines a preprocessing variable//Pre-processor is responsible for replacing the preprocessing variable in the program with its true value before the program compiles

C++primer Chapter II Part II

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.