C + + CONST Keyword Summary

Source: Internet
Author: User

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 Ten ; int Const ten;

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).

intA =Ten;Const int* p = &a;//What the pointer is pointing to cannot be changedint Const* p = &a;//Ibid .int*Constp = &a;//The pointer itself does not changeConst int*Constp = &a;//Neither of them can change .int Const*Constp = &a;//Ibid .

3. Modifying references

The following two definition forms are essentially the same:

int Ten ; Const int& b = A; int Const& b = A;

4. Modifier function Parameters

using the const modifier function parameter, the passed arguments cannot be changed within the function.

void func (constint& N) {     ten;        // }

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 to a 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:    intconst    {        //  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.

classa{ Public:    voidFunca () {}voidFUNCB ()Const {}};intmain{ConstA;    A.funca (); //can beA.FUNCB ();//Error    ConstA * b =NewA (); b->funca ();//can beB->FUNCB ();//Error}

9. Overloading member functions within a class

class a{public:    void  func () {}    voidconst {}   //  reload };

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.1415926535constdouble3.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 () {    constint;     int* p = const_cast<int*> (&a);      - ;    Std::cout<<*p;    //  -    std::cout<<a;     // 10, for the reason see part 10th }

C + + CONST Keyword Summary

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.