C + + member function declaration const has a role

1.

As anyone who has written several algorithm questions knows, when const is declared with built-in or custom types, it means that the constants cannot be changed.

For example, like the following.

const int N = 122222;

2.

Here, const and member function declarations are combined to represent what. This is also the introduction of this, which is equivalent to this-> bookNo

Look at this code

Assuming that bookNos is a string member variable
 // declare 
string isbn () const ;
 // define 
string isbn () const 
{ 
     bookNo = 1 ;
      return bookNo;        
}

This code will report an error, because when we declare a function, const is defined, which means that the function is read-only, in general, it can be modified.

This will make the code intuitive.

Guess you like

Origin www.cnblogs.com/rstz/p/12744618.html