Item 10.const Member Function

xiaoxiao2021-03-05  34

Item 10. Meaning of a const member function

1. What is the Const member function: member function name has a constant

2, the difference between Const member functions and Non-Const member functions: differences in the type of this pointer in member functions: The type of this pointer in the Non-Const member function is X * const, which point points to the unchanging THIS The THIS of the pointer Const member function is Const X * const, which is the point of the point and the content of this member function and the content of the class cannot be modified.

3. What should I do if I want the Const member function to modify non-statically data members? Plus Mutable keyword before non-statically data members

4. Use const to overload member functions class x {public: // ... int & operator [] (; const Int & operator [] (int index) const; // ...};

INT i = 12; x a; a [7] = i; // this is x * constconst x b; i = b [i]; // this is Const x * const

转载请注明原文地址:https://www.9cbs.com/read-37239.html

New Post(0)