C + + Const keyword Usage

Source: Internet
Author: User

"Reprint" Transfer from https://www.cnblogs.com/chogen/p/4574118.html

Const is a C + + language qualifier that restricts a variable from being allowed to change. The use of const can improve the security and reliability of the program to some extent. In addition, when looking at other people's code, clearly understand the role of Const, to understand the other side of the program also has some help.

1. Modifier Constants

Variables modified with const are immutable, and the following two definitions are essentially the same:

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

2. Modifier pointers

If the const is on the left side of the *, the const is used to modify the variable pointed to by the pointer, that is, the pointer is constant;
If the const is on the right side of *, const is the modifier pointer itself, that is, the pointer itself is a constant.
Therefore, it is more understandable to use the int const* p instead of the const int* p (although the two meanings are exactly the same).

int a = 10;const int* p = &a;            What the pointer is pointing to cannot be changed int const* p = &a;            Ibid. int* Const P = &a;            The pointer itself does not change to const int* Const P = &a;      Neither can be changed int const* const P = &a;      Ditto

3. Modifying references

The following two definition forms are essentially the same:

int a = 10;const int& B = a;int const& B = A;

4. Modifier function Parameters

With the const modifier function parameter, the passed argument cannot be changed within the function.

void func (const int& N) {     n = ten;        Compilation Error}

5. modifier function return value

the meaning of the return value with the const modifier function is basically the same as the meaning of the const modifier for the ordinary variable and the pointer.

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

6. Modifying class member variables

A const-decorated class member variable can only be assigned in the constructor initializer list of a class and cannot be assigned in the class constructor body.

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

7. Modifier class member functions

A const-Decorated class member function cannot change any member variable of the class object in the body of the function, nor can it call any non-const member function in the class.

Class A{public:    int& getValue () const    {        //A = ten;    Error        return A;    } Private:    int A;            Non-const member variable};

8. Modifying class objects

A const-decorated class object that cannot be modified by any member variable within that object.
Therefore, you cannot call any of the object's non-const member functions, because a call to a non-const member function has an attempt to modify the member variable.

Class a{public:    void Funca () {}    void Funcb () const {}};int main{    const A;    A.FUNCB ();    Can be    A.funca ();    Error    Const A * b = new A ();    B->FUNCB ();    Can be    B->funca ();    Error

9. Overloading member functions within a class

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

The difference between a const and a macro definition

Const constants have data types, and macro constants do not have data types. The compiler can perform type safety checks on the former, but only character substitution for the latter, no type security checks, and possible unexpected errors in character substitution.
Const constants from the assembly point of view, just give the corresponding memory address, instead of the immediate number as given in the definition of # #, so the const-defined constants are only one copy during the program run, and the constants defined by # define have several copies in memory.
In addition, the compiler typically does not allocate storage space for const constants, but instead saves them in the symbol table, which makes it a constant during compilation, without the storage and read memory operations, making it highly efficient.

#define PI 3.1415926535const Double pi = 3.1415926535;

Const_cast.

The const_cast operator is used to modify the const or volatile properties of a type.
A constant pointer is converted into a very good pointer, and still points to the original object;
The constant reference is converted to a very literal reference and still points to the original object.

void Func () {    const int a = ten;    int* p = const_cast<int*> (&a);    *p =;    std::cout<<*p;    std::cout<<a;     10, for the reason see part 10th}

C + + Const keyword Usage

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.