The usage of const in C++ is summarized in detail

  • 2020-04-02 01:42:36
  • OfStack

1. Const modifies ordinary variables and Pointers

Const modification variable, generally there are two ways to write:

Const TYPE value;
TYPE const value;

These two ways of writing are essentially the same. Const modifies the value of a variable of TYPE as immutable.

For a non-pointer TYPE TYPE, no matter how you write it, it means that value is only immutable.

Such as:

Const int nValue; / / nValue is const
Int const nValue; / / nValue is const

However, for TYPE of pointer TYPE, different writing methods will have different situations, such as:

A. const char * pContent;
Char * const pContent;
C. char const * pContent;
D. const char* const pContent;

Now, for the first three ways, we can rewrite it the other way, by putting parentheses around it

A. const (char) * pContent;
B. (char *) const pContent;
C. (char) const * pContent;

Just so you know. According to the rule for modifying non-pointer variables with const, it is obvious that A=C.


- for A,C, const modified variable of type char *pContent is constant, therefore, the content of pContent is constant immutable.

- there's another way to write B: const (char*) pContent;

Const modifier char* variable pContent is constant, so the pContent pointer itself is constant immutable.

-for D, it's actually A mixture of A and B, which means that the pointer itself and the contents of the pointer are both constant and immutable

Conclusion:

(1) the pointer itself is constant and immutable

(char *) const pContent;
Const pContent (char *);

(2) the contents of the pointer are constant and immutable

Const (char) * pContent;
(char) const * pContent;


(3) neither can be changed

Const char * const pContent;

There are some ways to distinguish between them:

Draw a line along the *,

If const is to the left of *, then const is used to modify the variable that the pointer points to, that is, the pointer points to a constant;

If const is to the right of *, const is the modifier pointer itself, that is, the pointer itself is constant.

2. Const modifies function parameters

One of its most common USES is the const modification of function parameters, which means that you cannot modify the value of the parameter in the function body (either the value of the parameter itself or the value contained in the parameter). It can be good

Void function (const int Var); The parameters passed by // cannot be changed in the function (nonsense, because Var is itself a parameter).

Void function (const char * Var); // parameter pointer to the content is constant immutable

Void function (const char * Var); // parameter pointer itself is constant immutable (also meaningless, because char* Var is also a parameter)

Parameters are referenced to increase efficiency and prevent modification.

When modifying reference parameters:

Void function (const Class & Var); // reference parameters cannot be changed within a function
Void function (const TYPE & Var); The // reference parameter is constant in the function

3. Return value of the const modifier

The const modifier return value is not used very much, and it has the same meaning as const modifies ordinary variables and Pointers.

(1) const int fun1() is meaningless, because the parameter return is itself an assignment.

(2) const int * fun2()

Const int *pValue = fun2();

We can think of fun2() as a variable, so this is what we call 1.

(3) int * const fun3 ()

Int * const pValue = fun2();

We can think of fun2() as a variable, which is the way we wrote 1.

4. Const modifies class object/object pointer/object reference

Const modifies a class object to indicate that the object is a constant object and that no member of it can be modified. The same is true for object Pointers and object references.

Const modifies an object in which any non-const member function cannot be called because any non-const member function has an attempt to modify a member variable.

Such as:


class AAA
{
void func1();
void func2() const;
}
const AAA aObj;
aObj.func1();  x 
aObj.func2();  correct 
 
const AAA* aObj = new AAA();
aObj->func1();  x 
aObj->func2();  correct 

5. Const modifies member variables

Const modifies a member function of a class that represents a member constant, cannot be modified, and can only be assigned in the initialization list.


class A
{
 ... 
const int nValue; //Member constants cannot be modified
 ... 
A(int x): nValue(x) {}; //Can only be assigned in the initialization list
}

6. Const modifies member functions

Const modifies a member function of a class that cannot modify any non-const member functions in the class. It's usually written at the end of the function.


class A
{
 ... 
void function()const; //It does not change the object's member variables. It cannot call any non-const member functions in the class.
}

For const class objects/Pointers/references, only the const member functions of the class can be called, so the most important function of const modifying member functions is to restrict the use of const objects.

7. Difference between const constant and define macro

(1) the compiler handles it differently

The define macro is expanded in the pre-process phase.

The const constant is used during the compile run.

(2) different types and safety checks

The define macro has no type, no type checking, just expansion.

Const constants have specific types, and type checking is performed at compile time.

(3) different storage modes

Define macros are just expanded, and you can use them as many times as you want without allocating memory.

The const constant is allocated in memory (either in the heap or on the stack).


Related articles: