A summary __c++ of C + + Const type usage

Source: Internet
Author: User
Tags modifier
Const type definition: Indicates that the value of a variable or object cannot be updated, and is introduced to replace precompiled directives

Constants must be initialized *************************

The role of Cons
(1) You can define const constants such as:
const int max=100;
int Array[max];
(2) Easy to type check for example:
void f (const int i) {...}
The compiler will know that I is a constant and does not allow modification;
(3) Can protect the modified things, prevent accidental modification, enhance the robustness of the program.
Or the above example, if you modify the function body I, the compiler will be an error;
For example:
void f (const int i) {i=10;//error!}
(5) Provides a reference for function overloading.
Class A
{
......
void f (int i) {...} file://a function
void f (int i) const {...} file://the overload of the previous function
......
};
(6) Can save space and avoid unnecessary memory allocation.
For example:
#define PI 3.14159 file://Chang
Const DOULBE pi=3.14159; file://does not put PI in ROM at this time
......
Double I=pi; file://allocates memory for PI at this time and is no longer assigned.
Double I=pi; FILE://for macro substitution during compilation, allocating memory
Double J=pi; file://No memory Allocations
Double J=pi; file://again to replace the macro and allocate memory again.
The const definition constant from the assembly point of view, just gives the corresponding memory address, rather than #define as given is the immediate number, so the const defined constants in the process of running the program only one copy, while the #define defined constants in memory have several copies.
(7) Improve the efficiency.
Instead of allocating storage space for ordinary const constants, the compiler saves them in a symbol table, which makes it a constant during compilation, without the operation of storing and reading memory, making it highly efficient.

Use const
(1) Modifying general constants, constant groups, and constant objects
The modifier const can be used before the type specifier, or it can be used in the type description end-of-file. 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 A; or a const A;

(2) Cosmetic pointer
const int *a; or int const *a; The const modifier points to an object that is mutable, and a points to an object that is not variable
int *const A; Const modifier pointer A, a immutable, a object variable
const int *const A; Pointers A and a point to objects that are not variable
(3) Cosmetic reference
Const Double & V; The object referenced by the reference cannot be updated
(4) The return value of the modifier function:
The const modifier can also modify the return value of a function, and the return value cannot be changed in the following format:
const int FUN1 ();
Const MyClass FUN2 ();
(5) Modifying the member function of a class:
The const modifier can also modify a member function of a class as follows:
Class ClassName
{
Public
int Fun () const;
.....
};
In this way, the data in the class cannot be modified when the function fun is invoked
(6) referencing a const constant in another connection file
extern const int i; The correct reference
extern const int j=10; Error. Constants cannot be assigned again



What are the limitations of constants placed inside a class.

Class A
{
Private
const int c3 = 7; Err
static int c4 = 7; Err
static const float c5 = 7; Err
......
};
Initializing constants inside a class
1 Initialization list:
Class A
{
Public
A (int i=0): Test (i) {}
Private
const int i;
};
2 external initialization, for example:
Class A
{
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.