The const is placed after the function to indicate that the function is a constant member function, and the constant member function is a function that cannot change the value of the member variable.
The const qualifier, which converts an object to a constant.
Example:
To make the meaning of the member function clearer, we can add a const description to the function prototype that does not change the member function of the object:
Class Point
{
Public
int GetX () const;
int GetY () const;
void setpt (int, int);
void offsetpt (int, int);
Private
int Xval, yval;
};
The const member function should add a const qualification to both the function prototype description and the function definition:
int point::gety () const
{
return yval;
}
Class Set {
Public
Set (void) {card = 0;}
BOOL Member (const int) const;
void Addelem (const int);
};
BOOL Set::member (const int elem) const
{
}
A const member function cannot be called by a constant member object because it may attempt to modify a constant's data member:
Const SET S;
S.addelem (10); Illegal: Addelem is not a constant member function
S.member (10); That's right
After the const is placed in the function