Summary of const usage in C + +

Source: Internet
Author: User
Tags modifier
1. Const modifier ordinary variables and pointers

const modifier variable, generally have two kinds of writing:

const TYPE value;

TYPE const value;

The two types of writing are essentially the same. It means that the variable value of the type with the Const modifier is immutable.

For a type that is not a pointer, no matter how it is written, it is a meaning that value is immutable.

For example:

const int Nvalue;//nvalue is const

int const nvalue;//Nvalue is const

However, for types of pointer type, different ways of writing can vary, for example:

A. const char *pcontent;

B. char * const pcontent;

C. Char const *pcontent;

D. const char* Const pcontent;


For the first three ways, we can put parentheses in a different way.

A. Const (char) *pcontent;

B. (char*) const pcontent;

C. (char) const *pcontent;

This is a clear glance. Based on the rules for const-modified non-pointer variables, it is obvious that a=c.


-For A,c, the const-decorated variable with the type char is *pcontent constant, so the pcontent content is constant.

-For B, there is actually a way of writing: const (char*) pcontent;

The meaning is: the const-decorated type is char* variable pcontent is constant, so the pcontent pointer itself is a constant variable.

-For D, is actually a mixture of a and B, indicating that both the pointer itself and the contents of the pointer are both constant and variable


Summarize:

(1) The pointer itself is a constant variable

(char*) const pcontent;

Const (char*) pcontent;


(2) What the pointer points to is a constant variable

Const (char) *pcontent;

(char) const *pcontent;


(3) Both are not variable

const char* Const pcontent;


There are also the ways to differentiate them:

Draw a line along the * number,

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

If the const is on the right side of the *, the const is the cosmetic pointer itself, that is, the pointer itself is a constant.

2. const modifier function parameters

The const modifier function parameter is one of its most extensive uses, which represents the value of a parameter that cannot be modified in the body of the function (including the value of the parameter itself or the value contained in the parameter). It can be very good

void function (const int Var); The passed arguments cannot be changed within a function (meaningless, because Var itself is a formal parameter).

void function (const char* Var); The parameter pointer refers to a constant variable

void function (char* const VAR); The parameter pointer itself is a constant variable (and meaningless because char* var is also a formal parameter)



parameter is referenced, in order to increase efficiency and prevent modification.

When a reference parameter is decorated:

void function (const class& VAR);//reference parameter cannot be changed within a function

void function (const type& Var); Reference parameters are invariant to constants within a function



3. const modifier function return value

The const modifier function returns a value that is not very useful, and its meaning is essentially the same as the const-modified ordinary variable and the meaning of the pointer.

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

(2) const int * FUN2 ()

const int *pvalue = FUN2 () at call time;

We can think of fun2 () as a variable, and that's what we call 1. (1) The writing, that is, the contents of the pointer can not be changed.

(3) int* const FUN3 ()

When called int * Const PVALUE = fun2 ();

We can think of fun2 () as a variable, and that's what we call 1. (2) The writing, that is, the pointer itself can not be changed.

4. Const modifier Class object/object pointer/object reference

The const-Decorated class object represents the object as a constant object in which no member can be modified. The same is true for object pointers and object references.

The const-decorated object, any non-const member function of the object, cannot be invoked because any non-const member function has an attempt to modify the member variable.

For example:

Class AAA

{
void Func1 ();

void Func2 () const;

}

Const AAA Aobj;

Aobj.func1 (); X

AOBJ.FUNC2 (); That's right



Const aaa* aobj = new AAA ();

Aobj->func1 (); X

AOBJ->FUNC2 (); That's right



5. const modifier member Variable

The const modifier member function of a class, which represents a member constant, cannot be modified, and it can only be assigned a value in the initialization list.


Class A

{

...

const int Nvalue; Member constants cannot be modified

...

A (int x): Nvalue (x) {}; You can only assign values in the initialization list

}



6. Const modifier member function

Const modifies a member function of a class, the member function cannot modify any of the non-const member functions in the class. Generally written at the end of the function to decorate.



Class A

{

...

void function () const; A constant member function that does not change the member variables of an object. Nor can you call any of the non-const member functions in the class.

}

For a Const class object/pointer/reference, only the const member function of the class can be invoked, so the most important function of the Const modifier is to restrict the use of the const object.



7. The difference between a const constant and a define macro definition

(1) The compiler handles different ways

Define macros are expanded during the preprocessing phase.

The const constant is used by the compile run phase.

(2) Different types and security checks

Define macros have no type, do not do any type checking, just expand.

The const constants have specific types that perform type checking during the compile phase.

(3) different storage modes

Define macros are only expanded, how many places to use, how many times to expand, will not allocate memory.

The const constants are allocated in memory (can be in the heap or in the stack).
Related Article

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.