The role of const in C + + in different locations of different objects (RPM)

Source: Internet
Author: User

http://blog.csdn.net/lzkit/article/details/7747741

Refer to the contents of the book and some ideas on the forum to list the following const main usage:

First, const and define.

Both can be used to define constants, but the const definition defines the type of the constant, so it is more accurate. #define只是简单的文本替换, in addition to defining constants, can also be used to define some simple functions, somewhat like inline functions (inline). Constants defined by const and define can be placed inside the header file. (Small bet: can be declared more than once, but only once)

Second, const and pointer, reference

(a) const and pointer.

Let's take a look at some of the following definitions:

int p;

const INT * P1=&P;//P1 variable, *P1 immutable, cannot be modified with *P1 at this time, but P1 can turn

The int * Const P2=&P;//P2 immutable, *P2 variable, allowing *P2 to modify its value at this time, but P2 cannot turn.

The const int *const p3=&me;//p3 Immutable, *P3 also immutable, at which point the value cannot be modified with *P3, nor can it be turned

(b) The difference between a pointer and a reference is simple: the reference is more concise and more secure. Because the reference declaration must be initialized. The reference is closer to the const pointer, and once associated with a variable, it will remain faithful to him.

(c) Const pointers can accept const and non-const addresses, but non-const pointers accept only non-const addresses. So the const pointer is more powerful, so use the const pointer as much as possible, which is a habit.   

Third, const and function

(a) const and pass-through value parameters

void function (const int Var); The passed arguments cannot be changed within the function.

(b) const and return values

const int Fuction1 (int);

The const value is returned here, meaning that the initial value of the variable returned in the original function cannot be modified, but the variable returned by the function is made into a copy, and can be modified without meaning, it may be assigned to any const or non-const type variable, There is absolutely no need to add this const keyword. But this is important only for internal types (because the intrinsic type is definitely a value, not a variable, not used as an lvalue), and for a user-defined type, the return value is constant.

(c) Const and member functions

void function (void) const; , the compiler checks this function, and any attempt to change member variables and invoke non-const member functions in this function is considered illegal

int Stack::functiont (void) const
{
+ + M_num; Compilation error, attempting to modify data member M_num
Pop (); Compile error, attempt to invoke non-const function
return m_num;
}

Third, Const and class

(a) The initialization of a const data member can only be performed in the initialization table of the class's constructor

Class A

{const int size = 100; Error};

(b) A const member function cannot be called by a constant member object because it may attempt to modify a constant's data member:

Class Set {

Public

Set (void) {card = 0;}

BOOL Member (const int) const;

void Addelem (const int);

//...

};

Const SET S;

S.addelem (10); Illegal: Addelem is not a constant member function

S.member (10); That's right

(c) member functions can implement overloads based on Const.

Iv. some suggestions for using the const

(a) bold use of the const will bring you endless benefits.

(b) To avoid the most general assignment error, such as assigning a const variable to a value.
(c) Using const in parameters should use a reference or pointer rather than a generic object instance.
(d) The three usages of const in member functions (parameters, return values, functions) are well used.
(e) Do not easily set the return value type of the function as const;
(f) In addition to overloaded operators, the return value type is not generally determined to be a const reference to an object;

The role of const in C + + in different locations of different objects (RPM)

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.