C + + Primer 5 Notes 3 Chapter 2 variables and basic types (2.4~2.6)

Source: Internet
Author: User

2.4 Const Qualifier
    1. Once the const object is initialized, his value does not change, so the const object must be initialized , which can be any complex expression, allowing runtime initialization

      const int j = get_size();
      const int i = 42;
    2. The primary limitation for Const objects is that operations that do not change their contents can only be performed on objects of the const type

    3. If you use an object to initialize another object, it does not matter whether the const object

    4. By default, a const object is only valid within a file, and when a const variable of the same name appears in multiple files, it is equivalent to defining a separate variable in a different file.

    5. If you need to define a const in a file, and you can use it in multiple files, workaround: Add an extern field for a const variable, whether it is a definition or a declaration

    6. For const, the C + + compiler handles the same way #define macro definitions, where it is worthwhile to replace the corresponding value with that variable. It also explains that after taking off the const with const_cast, there is no reason to change the Const.

2.4.1 Const Reference
    1. A reference to a constant cannot be used to modify the object to which he is bound

    2. The reference type must be consistent with the type of object it refers to, but

      1. When initializing a constant, allow any expression to be the initial value, as long as the expression result can be converted to a reference type, and, in particular, allow a constant reference to a const object, a literal value, or even a general expression , (at which point the binding is essentially a temporary object)
      2. A reference to a const might refer to an object that is not const, and const only qualifies the operation that refers to the reference, without restricting the original data
2.4.2 Pointers and const
    1. Similarly, the type of the pointer must be consistent with the type of object it is pointing to, but
      1. Allows a pointer to a constant to point to a const object, and const restricts only the value of the object that cannot be modified by this pointer.
      2. PS: The book says there are two exceptions, only to find the above <-_->!!
    2. Pointers and references to constants are merely pointers and references to "self-righteous"-they think they point to constants, so they consciously don't change the objects they refer to.

    3. The const pointer, pointing to the invariant, but pointing to the object content variable

2.4.3 Top-Level const
    1. The top-level const means that the pointer itself is a constant, the underlying const means that the pointer refers to a constant, more generally, the top-level const can indicate that any object is a constant

    2. The const that declares the reference is the underlying const

    3. When the copy operation is performed, the value of the copied object is not changed, so whether the copy of the baked object is a constant or not, but the underlying const cannot be ignored and the underlying const must be consistent when copying .

2.4.4 constexpr and constant expressions
    1. A constant expression is an expression that does not change and is able to get the results of a calculation during compilation.

    2. C++11 Specifies whether an expression that allows a variable to be declared as a constexpr type so that the compiler can validate the variable is a constant expression.

    3. Generally, if a variable is a constant expression, declare it as a constexpr type

    4. General constant expressions are more literal types, ex. arithmetic type, pointer, reference

    5. The initial value of a constexpr pointer must be either NULLPTR or 0, or an object stored in a fixed address. Ex. An object defined outside the body of a function, or a variable that is scoped beyond a function

    6. CONSTEXPR defines a top-level const, which holds the pointer to the invariant

2.5 processing Type 2.5.1 type alias
  1. You can use an alias declaration to top one type of alias:
    usINg S I =S aLes_ITem

  2. If a type alias refers to a composite type or constant, then using it in a declaration has a special effect. do not mistakenly try to replace the type alias with his original look to understand!!!

    typedefchar *pstring;    const0;   // cstr 是指向char的常量指针    const pstring * ps;       // ps 是个指针, 对象是指向char的常量指针    
2.5.2 Auto type specifier
    1. Auto enables the compiler to automatically parse the type that an expression belongs to

    2. Auto generally ignores the top-level const, preserves the underlying const, and if you want the inferred auto type to be a top-level const, you need to explicitly specify the const auto

2.5.3 Decltype Type Indicator
    1. The data type used to select and return operands, at which point the compiler only parses the expression to get the type, not the actual calculation

    2. Decltype the type returned, including the top-level const

    3. If the content of an expression is a dereference operation, get a reference type

    4. Decltype ((variable)) is referenced, and decltype (variable) gets the variable type

2.6 Custom Data Structures
    1. Do not put the definition of the class and the definition of the object together.

    2. C++11 allows you to provide a class-wide initial value for a data member

      struct Sales_date{std::string bookNo;unsigned0;double0.0;}
    3. Use precompiled directives to avoid duplicate header file inclusions, preprocessing variables ignoring scope rules

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer 5 Notes 3 Chapter 2 variables and basic types (2.4~2.6)

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.