http://blog.csdn.net/lihao21/article/details/8634876
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
[CPP]View PlainCopy
- 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
[CPP]View PlainCopy
- const screen Blankscreen;
- Blankscreen.display (); //Object read Operation
- 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,
[CPP]View PlainCopy
- Class Screen {
- Public
- Char get () const;
- };
When you define a const member function outside the class body, you must also add the const keyword, such as
[CPP]View PlainCopy
- Char screen::get () const {
- return _screen[_cursor];
- }
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
[CPP]View PlainCopy
- Class Screen {
- Public
- int OK () const {return _cursor;}
- int error (INTIVAL) const {_cursor = ival;}
- };
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 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
[CPP]View PlainCopy
- Class Name {
- Public
- void SetName (const string &s) const;
- Private
- Char *m_sname;
- };
- void SetName (const string &s) Const {
- M_sname = S.c_str (); //Error! Cannot modify m_sname;
- for (int i = 0; i < s.size (); ++i)
- M_sname[i] = s[i]; //bad style, but not 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,
[CPP]View PlainCopy
- Class Screen {
- Public
- Char get (int x,int y);
- Char get (int x,int y) const;
- };
In this case, the constant nature of the class object determines which function is called.
[CPP]View PlainCopy
- Const screen CS;
- Screen CC2;
- char ch = cs.get (0, 0); //Call the const member function
- ch = cs2.get (0, 0); //Call non-const member function
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.
Const class member functions for C + +