[Convert] const Usage After function declaration, const usage
We usually see some function declarations followed by a const, as shown below:
Void function () const {}
What does this const do?
Let's look at the example below in detail. The following two errors will be prompted directly before compilation.
# Include "stdafx. h "# include <iostream> using namespace std; class aa {int num; public: aa () {int B = 10; num = B ;}; void out1 () {cout <num <endl;} void out2 () const {cout <num <endl;} void out3 () const {num + = 10; // error, the const function cannot modify its data member cout <num <endl ;}; int main (int argc, _ TCHAR * argv []) {aa a1; a1.out1 (); a1.out2 (); a1.out3 (); const aa a2; a2.out1 (); // The const Member cannot access non-const functions a2.out2 (); a2.out3 (); return 0 ;}
In the Declaration and definition of class member functions,
The const function cannot modify its data members.
Const object. It cannot reference non-const member functions.
Reproduced in: http://www.cnblogs.com/xing901022/p/3413019.html