11, C ++ class member variables of const, const member functions

// reprint

Behind the increase const member functions of the class, this function does not indicate that the data members of the class object (precisely non-static data members) to make any changes.

In the design category, a principle that does not change the member functions for data member to be behind the increase in const, and for changing the data members can not add member functions const. So the const keyword for member functions of behavior were more clearly defined:

(1) a modified const member functions (const after the function refers to the parameter table, rather than in the front or the function parameter list), a member can only read data, data members can not be changed; no modification const member functions, data member is readable and writable.
(2) In addition, in the back of the class member functions const plus what good is it? That is constant (ie, const) object can call const member functions, and can not call non-const qualified function.

 

  • const member functions can access non-const object non-const data members, const data members, members also have access to all the data in the const object;
  • Non-const member functions an object can only be accessed any non-const data members (const can not access any of the data members of the object); . (See the above reasons can C ++ Primer (5th) 231 this page by default, this type is a pointer to a class type version of the constant pointer const, e.g. Screen class, this type Screen * cosnt. when the back member functions, plus const keyword, implicit this pointer will be modified to point i.e. const const * Screen class version of the constant type constant pointer. according to initialize principle, we can not assign a constant pointer to a const pointer) 
  • As a good programming style, in a statement when a member function, if the member function does not modify the data members, should the member function is declared const member functions as possible.

 

2. const Who is modified?

 Written const member functions, there are two

  1、void fun(int a,int b) const{}

  2、void const fun(int a,int b){}

Both versions are essentially: void fun (const type * this, int a, int b);

const is not modified and a parameter B; const is modified attribute this-> a and this-> b. Regardless of the const written position.

why?

Because this pointer c ++ class to do a hidden, in essence, const pointer modification is hidden this pointer points to memory space, this pointer is modified.

 

to sum up:

. 1) const const member functions they can access the data members of the object non-const, const data members, may access all data members in the object const;

2) non-const const member functions you can access the data members of the object non-const, const data members, but can not access any of the data members of the object const;

3) as a good programming style, a member function declaration, if the member function does not modify the operation of the data members, member functions should the const member functions declared as possible.

4) If only const member functions, you can call the non-const object is const member functions. When the const version and non-const member functions occur simultaneously versions, a non-const object to call a non-const member functions.

Guess you like

Origin www.cnblogs.com/tito/p/12371255.html