const keyword __c++ in C + +

Source: Internet
Author: User
Tags modifier
Why use Const. Code written in symbolic constants is easier to maintain; pointers are often moved by side-reading, rather than by writing; many function parameters are read-only and not written. The most common use of const is as the bounds of the array and the Switch-divided label (or in lieu of an enumerator), sorted as follows:
Constant variable: const type variable name or Type const variable name//both equals, such as: const int a=5; with int const a=5; Equivalent
Constant reference: const Type & Reference Name
Constant object: Type Const object Name
Regular member functions: Class Name:: Fun (formal parameter) const
Constant groups: Type const array name [size]
Constant pointer: const type* pointer name, type* const pointer name

The first hint is: in the constant variable (const type descriptor variable name), a constant reference (const type descriptor & reference name), constant (class name const object name), constant group (type descriptor const array name [size]), const "and" type specifier "or" Class name "( In fact, the class name is a custom type specifier where the location can be interchanged. Such as:

The role of a const

as shown in the following table:


Ii. The use of const

Constant variable: const type descriptor variable name
Constant reference: Const type descriptor & Reference name
Constant object: Class name const object name
Regular member functions: Class Name:: Fun (formal parameter) const
Constant groups: Type descriptor const array name [size]
Constant pointer: const type DESCRIPTOR * Pointer name, type descriptor * Const pointer name

2.1. Constant usage

Const type Varibale = value or Type const variable = value; In C + + is commonly used to replace the #define, one reason is that the compiler processing source code is removed by the preprocessor, error is very debugging; the second reason is that each time the use will reapply for memory, increase code, and const is only a memory space.

2.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
You can prevent users from modifying the return value. The return value should also be paid to a constant or constant pointer accordingly.

4. Const modifier member function

The const object can only access the const member function, not the const object, which accesses any member functions, including const member functions;
The members of the Const object cannot be modified, while objects maintained by the pointer can indeed be modified;
A const member function can not modify the data of an object, regardless of whether the object has a const nature. Compile-time checks based on whether to modify member 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.