[c++.gleaning] const keyword

Source: Internet
Author: User

semantics of the const keyword

Simply put, the const keyword will decorate a quantity as "constant", that is, the amount of non-volatile. This amount can be a basic type variable, a class object, a pointer, an object reference, a class member function in C + +.

Const Variable

The C++ standard stipulates that the Const keyword is placed before the type or variable name equivalent:

Const int  5;     <==>  int const  5;  const intNULL;  <==>  int constNULL;

Bjarne A mnemonic method in the C + + programming Language book: to read a const declaration from right to left, * to read pointer to:

Char*ConstP// p is a const pointer to charconst char * p;// P is a pointer to const char  Char* * p;// P is a pointer to pointer to charconst char * * p;// P is a pointer to pointer to const char  Char*Const* p;// P is a pointer to const pointer to charconst char *Const* p;// P is a pointer to const pointer to const char  Char**ConstP// p is a const pointer to pointer to charconst char **ConstP// p is a const pointer to pointer to const char  Char*Const*ConstP// p is a const pointer to const pointer to charconst char *Const*ConstP//P is a const pointer to const pointer to const char
Const member Functions

The const member function of the class indicates that the member function has read-only permission on the class member and does not have write permission. The objects of a class are divided into constant and extraordinary objects, and the member functions of a class are also divided into constant functions and very much functions. The mutual invocation relationship between the two is as follows:
•A constant function can be called by a very mass object;
•A very mass object can call a very mass function;
•A constant object cannot call a non-const function;
•A constant object can call a constant function;

[c++.gleaning] const keyword

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.