Any function that does not modify a data member should be declared as a const type. If you inadvertently modify the data member when you write the const member function, or if you call other non-const member functions, the compiler will indicate the error, which will undoubtedly improve the robustness of the program. In the following program, the member function GetCount of a class stack is used only for counting, and logically getcount should be a const function. The compiler will indicate an error in the GetCount function. Classstack{public:void Push (int elem), int Pop (void), intgetcount (void) const,//const member function Private:intm_num;int m_data[ 100];}; int Stack::getcount (void) const{++ m_num;//Compile error, attempt to modify data member M_numpop ();//Compile error, attempt to invoke non-const function returnm_num;} The declaration of the Const member function looks strange: The const keyword can only be placed at the end of a function declaration, presumably because other places are already occupied.
Const member function (GO)