C ++ const instructions

Source: Internet
Author: User

Transferred from:

Http://www.newsmth.net/pc/pccon.php? Id = 10002714 & nid = 359712

Http://blog.csdn.net/Eric_Jo/article/details/4138548

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.

********************** ***

Functions of cons
(1) const constants can be defined, for example:
Const int max = 100;
Int array [Max];
(2) Easy type check, for example:
Void F (const int I ){.........}
The compiler will know that I is a constant and cannot be modified;
(3) It 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 reports an error;
For example:
Void F (const int I) {I = 10; // error! }
(5) provides a reference for function overloading.
Class
{
......
Void F (int I) {...} file: // a function
Void F (int I) const {...} file: // reload of the previous function
......
};
(6) It can save space and avoid unnecessary memory allocation.
For example:
# Define PI 3.14159 file: // constant macro
Const doulbe Pi = 3.14159; file: // pi is not put into ROM at this time
......
Double I = PI; file: // at this time, the PI is allocated with memory and will not be allocated later!
Double I = PI; file: // macro replacement during compilation and Memory Allocation
Double J = PI; file: // no memory allocation
Double J = PI; file: // 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 as # 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.
(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.

Use const
(1) Modify common constants, regular arrays, 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 & V; the referenced object cannot be updated.
(4) the return value of the modifier:
The const modifier can also modify the return value of a function, which cannot be changed. The format is as follows:
Const int fun1 ();
Const myclass fun2 ();
(5) modify the member functions 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 when the function fun is called.
(6) reference the const constant in another connection file
Extern const int I; // correct reference
Extern const Int J = 10; // error! Constants cannot be assigned another value.



* ******************* What are the restrictions on constants placed inside the class?

Class
{
PRIVATE:
Const int C3 = 7; // err
Static int C4 = 7; // err
Static const float C5 = 7; // err
......
};
Constants inside the initialization class
1. Initialization list:
Class
{
Public:
A (INT I = 0): Test (I ){}
PRIVATE:
Const int I;
};
2. External initialization, for example:
Class
{
Public:
A (){}
PRIVATE:
Static const int I;
};
Const int A: I = 3;

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.