Const constant Type

Source: Internet
Author: User

1. Definition: A const constant type represents a "constant value variable" whose value is a variable that cannot be modified. That is, once the variable is declared as a const type, the compiler will disallow any action that attempts to modify the variable.

2. Disclaimer: Const < declares data type > constant or constant expression initialization.

3. The difference from the macro definition Directive # define:

#define只是做简单的文本替换, type checking is not done.

When a const declaration is made, the compiler replaces the variable with a strict type check, only if it is the same as the declared data type.

4. Common uses:

Use the const-qualified function's arguments and return values.

1) The argument cannot be modified in a function when the parameter of the function is limited to the const type.

2) When the return value of a function is qualified with a const,

If the return value of a function is a base type (such as int), there is no special meaning at this time.

If a function returns a pointer or reference, at which point the function is called, the return value cannot be used to alter the variable that the return value refers to or refers to.

3) The modification operation of the qualified member function.

Ps:http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html (modified version)

1. Const modifier common variables and pointers

Const-Modified variables are generally written in two ways:

const TYPE value;

TYPE const value;

The two formulations are essentially the same. The meaning of this is that the const-modified variant of type ' value ' is immutable.

For a non-pointer type, 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, there are different ways to style a pointer type, for example:

A. const char *pcontent;

B. char * const pcontent;

C. Char const *pcontent;

D. const char* Const pcontent;


For the first three types of writing, we can add parentheses to it in a different way.

A. Const (char) *pcontent;

B. (char*) const pcontent;

C. (char) const *pcontent;

This is a glance. According to the rules for const modifiers of non-pointer variables, it is obvious that a=c.


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

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

A const-modified variable of type char* is pcontent as a constant, so the pcontent pointer itself is constant immutable.

-For D, it is actually a and b mixture, indicating that both the pointer itself and the pointer content are constant immutable


Summarize:

(1) The pointer itself is constant immutable
(char*) const pcontent;
(2) The content pointed to by the pointer is a constant variable
Const (char) *pcontent;
(char) const *pcontent;

(3) Both are not variable
const char* Const pcontent;
There are different ways of doing this:

Line along the * number,

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

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

2. const modifier function parameters

The Const modifier parameter is one of its broadest uses, which indicates that the value of the parameter 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 the function (meaningless because Var itself is a formal parameter).

void function (const char* Var); The contents of the parameter pointer are constant

void function (char* const VAR); The parameter pointer itself is a constant invariant (also meaningless, since char* var is also a formal parameter)



Parameters are references, in order to increase efficiency and to prevent modification.

When modifying reference parameters:

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

void function (const type& Var); The reference parameter is constant within the function is not variable



3. const modifier function return value

The const modifier function returns a value that is not practical, and its meaning is basically the same as the const-modified ordinary variable and the pointer.

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

(2) const int * FUN2 ()

Called when const int *pvalue = FUN2 ();

We can think of fun2 () as a variable, and that's what we call 1. (1) The notation, that is, the pointer content is immutable.

(3) int* const FUN3 ()

Called when int * Const PVALUE = FUN3 ();

We can think of Fun3 () as a variable, and that's what we call 1. (2) The notation, that is, the pointer itself is immutable.

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

The const-Decorated class object indicates that the object is a constant object, and any of its members cannot be modified. The same is true for object pointers and object references.

Const-Decorated object, any non-const member function of the object cannot be called 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-&GT;FUNC2 (); That's right



5. const modifier member Variable

The const-decorated class's member variable, which represents a member constant, cannot be modified, and it can only be assigned to 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 member variable in the class and cannot call any non-const member function


Class A

{

...

void function () const; The constant member function, which does not change the member variable of an object. You cannot call any of the non-const member functions in the class.

}

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



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

(1) different compiler processing methods

Define macros are expanded during the preprocessing phase.

Const constants are used during the compile run phase.

(2) Different types and security checks

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

Const constants have specific types that perform type checking during the compilation phase.

(3) different storage methods

The Define macro is only expanded, how many places are used, how many times it is expanded, and no memory is allocated.

Const constants are allocated in memory (either in the heap or in the stack).

Const constant Type

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.