Role of the const keyword

Source: Internet
Author: User

Const Type Definition: indicates that the value of a variable or object cannot be updated. The purpose of this definition is to replace the precompiled command.

×××××× Constant must be initialized.

Role of const

A can define const constants, such

Const int max = 100;

Int array [Max];

B facilitates type check, as shown in figure

Void F (const int I ){...}

The compiler will know that I is a constant and cannot be modified.

C can protect modified things, prevent accidental modifications, and enhance program robustness.

In the above example, if I is modified in the function body, the compiler will report an error, for example, void F (const int I) {I = 10; // error}

D. provide a reference for function overloading (this is not very understandable)

Class

{

...

Void F (int I) {...} file; // a function

Void F (int I) const {...} file: // reload of the previous function

...

}

E can save space and avoid unnecessary Memory Allocation

For example:

# Define PI 3.14159 // constant macro

Const double 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

Const defines constants. From the assembly point of view, only the corresponding memory address is given, rather than the number of immediate values 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.

F. Improve 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, to improve its efficiency.

 

Use const

(1) Modify common constants, constant groups, and common objects

The modifier const can be used before or after the type specifier, for example:

Int const x = 2; or const int x = 2;

Int const A [5] = {1, 2, 3, 4, 5}; or const int A [5] = {1, 2, 3, 4, 5 };

Class A; const A; or a const;

(2) modify the pointer

Const int * A; or Int const * A; // The object indicated by const. A is variable, and the object indicated by a is immutable.

Int * const A; // const modifier pointer A, A is immutable, and a points to the object is variable

Const int * const A; // The objects pointed to by pointers A and A are not changeable.

(3) Modify reference

Const double & A; // The object referenced by this reference cannot be updated.

(4) modify the return value of a function

The const modifier can also modify the return value of a function, which cannot be changed. The format is as follows:

Const int fun1 ();

(5) modify the member variables of the class

The const modifier can also modify the member functions of the class in the following format:

Class classname

{

Public:

Int fun () const;

....

};

In this way, the data in the class cannot be modified in function fun.

(6) reference the const constant in another connection file

Extern const int I; // correct reference

Extern const Int J = 10; // error. The constant cannot be assigned again.

 

What are the restrictions on variables placed inside the class?

Assign values when the variables placed inside the class cannot be defined. You can initialize values in the class constructor initialization list or outside.

Class

{

PRIVATE:

Const int C3 = 7; // Error

Static int C4 = 7; // Error

Static const float C5 = 9; // Error

....

};

Initialize variables in the class

1. Initialization list:

Class

{

Public:

A (INT I = 0): Test (I ){}

PRIVATE:

Const int test;

};

2. External Initialization

Class

{

Public:

Static const int I;

};

Static const int A: I = 3;

 

The const modifier can change an object to a constant object. What does it mean?

That is to say, the value of the variable modified using const cannot be modified again at any position of the program, just like a constant!

Let's take a look at the two examples below

Void main (void)

{

Const int A = 10;

Int B = 20;

 

Const int * PI;

Pi = &;

Cout <* pI <"|" <A <Endl;
Pi = & B;

Cout <* pI <"|" <B <Endl;

}

The most important sentence in the above Code is const int * pi.

This sentence is read from the right to the left: PI is a pointer to an int type object defined as Const.

Such a declaration can modify the memory address pointed to by the PI pointer, but cannot modify the value pointing to the object;

If * Pi = 10 is added after the Code, such a value assignment operation is not allowed to be compiled.

 

Void main (void)

{

Int A = 10;

Const int * const Pi = &;

Cout <* pI <"|" <A <Endl;

}

The most important sentence above is const int * const PI;

This sentence is read from the right to the left: PI is a const pointer pointing to an int type object;

The purpose of such a declaration is that you cannot modify the memory address of the object pointed to by PI, nor modify the object value by using the pointer's unreferencing method, that is, * Pi = 10.

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.