The third week of learning: constant objects, constant member functions

  1. Constant object : do not want the value of an object to be changed, add the const keyword when defining the object. Constant objects cannot execute non-constant member functions
    const class_name name
  2. Constant member function : add const after the member function. During execution, the object it acts on should not be modified, and other member variables except static member variables cannot be modified, nor can it be called non-constant member functions of the same kind.
    T func( ) const
  3. Constant member function overloading : The name and parameters are the same, but one is const and the other is not, even if it is overloaded. One is called by a non-constant object, and one is called by a constant object
  4. Frequent references : often used as function parameters

Guess you like

Origin blog.csdn.net/ZmJ6666/article/details/108557374