C ++ const and const

Source: Internet
Author: User

C ++ const and const

 

The usage of the const keyword in C ++ is very flexible, and the use of const will greatly improve the robustness of the program. I will summarize the following information based on various aspects and hope to help my friends.

Const is a common type modifier in C ++. A common type is a type indicated by a const. The values of variables or objects of a common type cannot be updated.

No.

Function

Description

Reference Code

1

Const constants can be defined.

Const int Max = 100;

2

Easy type check

Const constants have data types, while macro constants do not. The compiler can perform type security checks on the former, while the latter only performs character replacement without type security checks, and unexpected errors may occur during character replacement.

Void f (const int I ){.........}
// Check the type of the input parameter. If the type does not match, a prompt is displayed.

3

Can protect modified things

Prevents unexpected modifications and enhances program robustness.

Void f (const int I) {I = 10; // error! }
// If I is modified in the function body, the compiler reports an error.

4

You can easily adjust and modify parameters.

Same as macro definition, it can be changed as long as it remains unchanged.

5

Provides a reference for function overloading.

Class
{
......
Void f (int I) {...} // a function
Void f (int I) const {...} // overload of the previous function
......
};

6

Saves space and avoids unnecessary memory allocation.

Const defines constants. From the assembly point of view, only the corresponding memory address is given, rather than the number of immediate values as # define. Therefore, the constant defined by const has only one copy during the program running, while # The constant defined by define has several copies in the memory.

# Define PI 3.14159 // constant macro
Const doulbe Pi = 3.14159; // Pi is not put into ROM at this time
......
Double I = Pi; // at this time, the Pi is allocated with memory and will not be allocated later!
Double I = PI; // macro replacement during compilation and Memory Allocation
Double j = Pi; // no memory allocation
Double J = PI; // macro replacement, memory allocation again!

7

Improved efficiency

Generally, the compiler does not allocate storage space for common const constants, but stores them in the symbol table, which makes it a constant during compilation without the operation of storage and read memory, this makes it highly efficient.

Int const * p = & a; // equivalent const int * p = & a; // the pointer refers to a constant. p can modify int * const p; // The pointer is a constant and the value pointed to can be modified.
 
class WORK{public:     const int NUM;    int a;    WORK():NUM(3)   //const var must init in init_list    {        a=1;    }    void change()    {        a =2;    }    void disp()const    {        cout<<"const NUM="<<NUM<<endl;        //change();  //const function can not call non_function        //a=3;  const function can not modify member variable    }};

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.