Summary of C ++ const keywords and const keywords

Source: Internet
Author: User

Summary of C ++ const keywords and const keywords

Const is a qualifier of the C ++ language, which limits that a variable cannot be changed. Using const can improve the security and reliability of the program to a certain extent. In addition, while watching other people's code, you can clearly understand the role of const, which is helpful for understanding the program of the other party.

1. Modify Constants

Variables modified with const are unchangeable. The following two definitions are essentially the same:

const int a = 10;int const a = 10;

2. Modify pointer

If the const is on the left side of *, const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant;
If const is on the right side of *, const is to modify the pointer itself, that is, the pointer itself is a constant.
Therefore, we recommend that you use int const * p instead of const int * p (although both have the same meaning), which is easier to understand.

Int a = 10; const int * p = & a; // The content pointed to by the pointer cannot be changed to int const * p = & a; // same as int * const p = &; // The pointer itself cannot be changed to const int * const p = & a; // neither can be changed to int const * const p = & a; // same as above

3. Modify reference

The following two definitions are essentially the same:

int a = 10;const int& b = a;int const& b = a;

4. Modify function parameters

Use const to modify function parameters. The passed parameters cannot be changed within the function.

Void func (const int & n) {n = 10; // compilation error}

5. Modify the function return value

The meaning of modifying the function return value with const is basically the same as that of modifying common variables and pointers with const.

Const int * func () // The content pointed to by the returned pointer cannot be modified {// return p ;}

6. Modify the class member variables

The class member variables modified with const can only be assigned values in the class constructor initialization list, but not in the class constructor.

Class A {public: A (int x): a (x) // correct {// a = x; // error}
Private: const int ;};

7. Modify member functions

A class member function modified with const cannot change any member variables of the class object in the function body, nor call any non-const member function in the class.

Class A {public: int & getValue () const {// a = 10; // return a;} private: int a; // non-const member variable };

8. Modify Class Object

For class objects modified with const, no member variables in the object can be modified.
Therefore, you cannot call any non-const member functions of this object, because calls to non-const member functions may attempt to modify member variables.

Class A {public: void funcA () {}void funcB () const {}}; int main {const A;. funcB (); //. funcA (); // error const A * B = new A (); B-> funcB (); // B-> funcA (); // error}

9. Reload member functions in the class

Class A {public: void func () {} void func () const {} // reload };

10. Difference Between const and macro definition

Const constants have data types, while macro constants do not. The compiler can perform a type security check on the former, while the latter only performs character replacement without the type security check. In addition, unexpected errors may occur during character replacement.
Const constants only provide the corresponding memory address from the assembly point of view, rather than giving the immediate number like # define. Therefore, the const-defined constants have only one copy during the program running, while the # define-defined constants have several copies in the memory.
In addition, the compiler usually saves const constants in the symbol table instead of allocating storage space. This makes them a constant during compilation, without the operation of storage and read memory, this makes it highly efficient.

#define PI 3.1415926535const double PI = 3.1415926535;

11. const_cast

The const_cast operator is used to modify the const or volatile attributes of the type.
1. Constant pointers are converted to very large pointers and still point to the original object;
2. constant reference is converted into a very large number of references and still points to the original object.

Void func () {const int a = 10; int * p = const_cast <int *> (& a); * p = 20; std: cout <* p; // 20 std: cout <a; // 10. For the reason, see section 10th}

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.