C ++ Memorandum: const

Source: Internet
Author: User
Document directory
  • Const variable Declaration
  • Const Parameters
  • Const Return Value
  • Const pointer
  • Const (left value) Reference
  • Const data member
  • Const member functions
  • Const iterator
  • Const_cast
  • Const smart pointer
  • Implicit const in lambda expressions
  • Const right reference
  • Constexpr
The const variable Declaration uses const in the variable declaration to indicate that the variable has a read-only constant attribute. unless declared with extern, the variable must have an initial value and cannot be modified after the declaration.
const int n = 0;n = 1; //compile error

 

When the const parameter is used in the parameter list, it indicates that the parameter has a read-only constant attribute inside the function, that is, it cannot be modified.
void f(const int& n){    n = 1;//compile error}

 

The const return value indicates that the function return value has a read-only constant attribute and cannot be modified after it is accepted. It is usually used to return a constant static volume, global volume, or class member. Const pointer const is the most confusing combination of const and pointer, because the type and pointer pointed to by the pointer can be modified by const.
  • Const char *
    Pointer to the character constant (pointer to const char). Note that the const modifier type char points to a constant and cannot be modified. The pointer is very large and can be modified.
  • Char const *
    It is equivalent to const char *. Note that const is placed before and after the char type, and the char type is also modified.
  • Char * const
    Const pointer to Char. Note that the const modifier type char * indicates that the pointer points to a large number of characters, which can be modified. The pointer is a constant and cannot be modified.
  • Const char * const
    Const pointer to const Char. Note that the const modifier type const char * is a constant and cannot be modified.
Const (left value) is referenced in C ++. A reference can be considered as a constant pointer with a non-null initial value and points syntax.
  • Const string &
    Reference to a constant string. Note that the const modifier type string indicates that the string to be referenced is a constant and cannot be modified.
  • String const &
    It is equivalent to const string &. Note that const is placed before and after the type string, and the type string is also modified.
The data member of the const data member class uses const to indicate that the data member is a constant and will not be modified after the const Initialization is complete. The member function of the const member function class uses const to indicate that the member function will not modify any data member (except if mutable keyword is used ). Const iterators are the product of generalization of pointers in the standard library. Therefore, const iterators are similar to const pointers.
  • Vector <char >:: const_iterator
    The iterator that points to the character constants in the vector container. The iterator points to a constant and cannot be modified. The iterator is very large and can be modified.
  • Vector <char>: const_reverse_iterator
    The reverse iterator that points to the character constants in the vector container. The iterator points to a constant and cannot be modified. The iterator is very large and can be modified.
The const_cast keyword can be used to remove the constant attributes of the const variable.
Due to the existence of const_cast, the entity declared by const may actually be modified. It is a variable with a constant attribute rather than a constant in the absolute sense. The const smart pointer has been added to the C ++ 0x standard library's smart pointer type (such as shared_ptr). It is similar to the const pointer after being modified with Const.
  • Shared_ptr <const char>
    A smart pointer to a character constant. Note that the const modifier type char points to a constant and cannot be modified, but the pointer is very large and can be modified.
  • Const shared_ptr <char>
    The smart pointer constant pointing to the character. Note that const modifies the smart pointer of the char type. The pointer points to a very large number of characters and can be modified. The pointer is a constant and cannot be modified.
  • Const shared_ptr <const char>
    A smart pointer constant pointing to a character constant. Note that const modifies both char and char smart pointers. The characters and pointers pointed to by the pointer are constants and cannot be modified.
The implicit const In the lambda expression enters the lambda expression function through value capture. The variables by default have the const attribute. to modify the variable, the mutable keyword must be added.
The following code cannot be compiled because Variable N is a constant in the lambda expression and cannot be modified.
int n = 0;[=]{    ++n;//compile error}();

The mutable keyword can be modified.

int n = 0;[=]()mutable{    ++n;//n == 1}(); //n == 0
The right-value reference of const is a new reference introduced by the C ++ 0x standard. The binding of the right-value solves the problem of mobile semantics and perfect forwarding.
To solve the mobile semantics problem, you need to move (steal) the right value to reference the resource pointing to the right value, but the right value reference cannot be modified after being modified by const, therefore, the right-value reference of const is almost useless.

  • Const string &&
    Reference to the right value of a constant string. Note that the const modifier type is string. The referenced string is a constant and cannot be modified.
  • String const &&
    It is equivalent to const string &. Note that const is the same before and after the type string, and the type string is also modified.
Constexpr is a new keyword in the C ++ 0x standard. Unlike const, the entity declared with constexpr is a real constant, and its value cannot be modified after the initialization is completed during the compilation period. In addition, constexpr extends the concept of constants during the compilation period to constants of the User-Defined type and constant functions during the compilation period, and the unchangeable modification of declared constants is ensured by the compiler, therefore, it can be said that the constant declared by constexpr is a general and guaranteed constant expression.
Related Article

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.