We know that in C + +, if a variable is declared as a const type, the operation that attempts to modify the value of the variable is considered a compilation error. For example
Const Char blank='; blank='\ n'; // Error
In object-oriented programming, in order to embody encapsulation, the data members of the class object are not usually allowed to be modified directly. To modify a class object, you should call the public member function to complete. To guarantee the Const object's constant nature, the compiler must distinguish between unsafe and secure member functions (that is, a function that attempts to modify a class object and not modify a class object). For example
Const Screen Blankscreen; //screenfor Class,blankscreen for its object blankscreen.display (); // the read operation of the object blankscreen.set ('*'); // Error: Const class object does not allow modification
In C + +, only member functions declared as const can be called by a Const class object. To declare a class member function of a const type, simply add the keyword const after the member function argument list, for example,
1 class Screen { 2public: 3 char get() const; 4 };
When you define a const member function outside the class body, you must also add the const keyword, such as
1 Char Screen::get() const { 2 return _screen[_cursor]; 3 }
If you declare a member member function as const, the function does not allow you to modify the data members of the class. For example
1 class Screen { 2public: 3int OK () const {return _cursor;} 4int error (int ival) const {_cursor = ival;} 5 };
In the definition of the member function above, OK () is defined as valid, and the definition of error () is illegal.
It is important to note that declaring a member function as const guarantees that the member function does not modify the data member, but if the data member is a pointer, the const member function does not guarantee that the object pointed to by the pointer is not modified and the compiler does not detect the modification as an error. For example
1 className {2 Public: 3 voidSetName (Conststring&s)Const; 4 Private: 5Char*M_sname; 6 }; 7 8 voidSetName (Conststring&s)Const { 9M_sname = S.c_str ();//Error! Cannot modify m_sname; Ten One for(inti =0; I < s.size (); ++i) AM_sname[i] = s[i];//bad style, but not the wrong -}
Although M_name cannot be modified, but M_sname is a char * type, the const member function can modify the character it points to.
A const member function can be overloaded with a non-const member function that has the same argument list, for example,
1 class &NBSP;SCREEN&NBSP;{&NBSP;&NBSP; 2 Span style= "color: #0000ff;" >public :&NBSP;&NBSP; 3 char get (int x,< Span style= "color: #0000ff;" >int 4 Span style= "color: #0000ff;" >char get (int x,< Span style= "color: #0000ff;" >int y) const ; 5 };
1 const screen cs; Const object 2 screen cc2; // non-const object 3 get (0 , 0 ); < Span style= "color: #008000;" >// calls the const member function 4 CH&NBSP;=&NBSP;CS2. get (0 , // calls non-const member functions
Summary:
1) The const member function can access non-const data members of non-const objects, const data members, or access all data members within a const object;
2) Non-const member functions can access non-const data members of non-const objects, const data members, but cannot access arbitrary data members of Const objects;
3) as a good programming style, when declaring a member function, if the member function does not modify the data member, the member function should be declared as a const member function whenever possible.
Transferred from: http://blog.csdn.net/lihao21/article/details/8634876
Const class member function for "go" C + +