In C ++ const usage abbreviated

Content from the Internet, would like to finish this collection

const basis

If the const keyword does not relate to a pointer, we well understand, here is the situation involving pointers:

int b = 500;
const int* a = &b; [1]
int const* a = &b; [2]
int* const a = &b; [3]
const int* const a = &b; [4]

There are four cases actually looks are only two:

  • Const qualifier and the relative position of the variable type name does not affect its role

If const asterisk on the left side, the const is used to modify the variable pointer points to , i.e. the pointer to be constant;

If const asterisk on the right, is modified const pointer itself , that is, the pointer itself is constant.

Thus, [1] and the case [2] is the same, the contents are constant pointer is not allowed to change the value of the variable operation of this case, if not * a = 3;

[3] The pointer itself is constant, and the contents of the pointer points is not constant, changes the pointer itself can not operate this case, as a ++ is wrong;

[4] The content itself and the pointer points are constants.

In addition some powerful features is its use of const in the function declaration. In a function declaration, const return value of the function can be modified, or a parameter; for member functions can also be modified is the entire function. Following situations:
int & operator = (const int & A);
void Fun0 (A const int *);
void fun1 () const; // fun1 () must be a class member function
const int fun2 ();

const initialization

Look at the situation const variable initialization

const constants:

int b;
const int a = b;

const constant pointer:

int* d = new int();
const int* c = d;
const int* c = new int();

const constant reference:

int f;
const int& e = f;

Often const object reference:

M1 MyClass;
const MyClass & M2 = M1;
// can only access such as e declared as const function, and can not access ordinary member functions;

[Thinking 1]: The following assignment of this method correct?
* c = new new int const int ();
int * E = c;
[thinking 2]: The following assignment of this method correct?
new new = C * const int int ();
int * B = C;

As parameters and return values

In fact, both parameters or return values, the reason is the same, time and time parameters passed function returns, const variable initialization

Const parameter modification

void fun0(const int* a );
void fun1(const int& a);

Function call time, with the corresponding variable initialization const constants, then the body of the function performed often quantified according const the modified portion, as parameter is const int * a, it can not pass in the pointer contents change, protection of the contents of the original pointer points; parameter such as const int & a, it can not reference objects passed in change, the protective properties of the original object.

[Note]: Parameter const argument is typically used when a pointer or reference;

Modification of the return value const

const int fun2( );
const int* fun3( );

After this statement the return value, const accordance with the "modification principle" was modified accordingly play a protective role.

const rational operator*(const rational& lhs, const rational& rhs)
{
return rational(lhs.numerator() * rhs.numerator(),
lhs.denominator() * rhs.denominator());
}

Return Value const modifications can be prevented by allowing such operations occur:
Rational A, B;
radional C;
(A * B) C =;

General use const Returns the object in itself is used for two purposes of overloaded functions and operators when a new object is modified.

[Summary]
In general, the function returns the value of an object, if it is declared const, used for operator overloading. In general, the modification is not recommended to use const return type of situation or an object reference to an object of value function.
The following reasons:
If the return value is an object reference to an object or const const is, the return value has const attribute, public data members of a class instance can be accessed and the const member function returns, and does not allow for carry out its assignment, which is rarely used in general.

[Reflections]: this definition the assignment operator overloads can it?
const a & operator = (const a & a);

The use of class member function const

Usually on the body of the function, the form: void fun () const;

If a member function does not modify the data members , it is best to declare it as const, because the data do not allow members to modify const member functions, if modified, the compiler will report an error, which greatly improves the robustness of the program.

Some recommend the use of const

1, to bold use const, which will give you endless benefits, but only if you have to figure out the whole story;

2, to avoid the most common assignment errors, such as the const variable assignment, concrete and tangible Questions;

3, in the parameter const reference or pointer should be used, rather than the general object instance, for the same reason;

4, const member functions in three uses to good use;

5, do not easily return type of the function

6, in addition to the general not overloaded operators type as the return value const reference to an object;

Guess you like

Origin www.cnblogs.com/tc43/p/cpp_const.html