The use of const in C + + _c language

Source: Internet
Author: User
Tags modifier volatile

Const is a commonly used type modifier in C + +, which refers to a type that is described using the type modifier const, and the value of a variable or object of a constant type cannot be updated.

1. Defining constants
(1) The const modifier variable, the following two kinds of definition form are essentially the same. It means that the variable value of the type with the Const modifier is immutable.

TYPE const VALUENAME = value;
Const TYPE VALUENAME = value;


(2) Change the const to an external connection for expansion to global, the memory is allocated at compile time, and can be uninitialized, just as a declaration, which the compiler considers to be defined elsewhere in the program.

Extend const int valuename = value;

2, the use of const pointer
(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;

(4) There are also different ways to 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.

3. Use const in function

(1) const modifier function parameters
A. Passing parameters cannot be changed within a function (meaningless because Var itself is a formal parameter)

void function (const int Var);

B. The parameter pointer refers to a constant variable

void function (const char* Var);

C. The parameter pointer itself is a constant variable (and meaningless because char* var is also a formal parameter)

void function (char* const VAR);

D. parameter as reference, in order to increase efficiency and prevent modification. When a reference parameter is decorated:

void function (const class& Var); Reference parameters cannot be changed within a function

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

2) 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.
A.const int fun1 ()//This is actually meaningless because the parameter return itself is an assignment.
b. const int * FUN2 ()//call when const int *pvalue = FUN2 ();
We can think of fun2 () as a variable, that is, the contents of the pointer are immutable.
c.int* Const FUN3 ()//Call time int * Const PVALUE = fun2 ();
We can think of fun2 () as a variable, that is, the pointer itself is immutable.

4, class-related const

(1) 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
}

(2) Const modified 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.
}

(3) Const modifier class object/object pointer/object reference

The const decorated class object indicates that the object is a constant object, and any member of it 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 invoked because any non-const member function has an attempt to modify a member variable.

For example:

Copy Code code as follows:

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, the conversion of the const type to a non-const type method

C + + provides four conversion operators:

Const Case <new type> (expression)
static_cast <new_type> (expression)
reinterpret_cast <new_type> (expression)
dynamic_cast <new_type> (expression)

Non-const type conversions are performed using Const_cast.
Usage: const_cast <type_id> (expression)
This operator is used to modify the const or volatile property of a type. In addition to the const or volatile modification, the type_id and expression are of the same type.

• The constant pointer is converted to a very measured pointer and still points to the original object;
• The constant reference is converted to a very literal reference and still points to the original object;
• The constant object is converted to a very measured object.

Copy Code code as follows:

const INT constant = 21;
Const int* const_p = &constant;
int* modifier = const_cast<int*> (const_p);
*modifier = 7;

Of course we can use the following traditional way to replace:
Copy Code code as follows:

const INT constant = 21;
int* modifier = (int*) (&constant);

As we've seen from the previous code, we can't modify the constant, but we can reassign the modifier.

But is the program world really messed up? Did we really change the value of Constatn through modifier? Modifying the const variable data is really the purpose of C + + to const?

If we print out the results:

Copy Code code as follows:

cout << "constant:" << constant <<endl;
cout << "const_p:" << *const_p <<endl;
cout << "modifier:" << *modifier <<endl;
/**
Constant:21
Const_p:7
Modifier:7
**/

Constant still retains its original value.

But they did point to the same address:

Copy Code code as follows:

cout << "constant:" << &constant <<endl;
cout << "const_p:" << const_p <<endl;
cout << "modifier:" << modifier <<endl;

/**
constant:0x7fff5fbff72c
const_p:0x7fff5fbff72c
modifier:0x7fff5fbff72c
**/

Although this allows you to reassign a const value, never reassign the const data.

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.