If a object is declared as const it can read the data member using const member function but can not alter the data member. And if we want to alter the data member using the const object then the data member must be declared as mutable.
Example:
class Test
{
mutable int x;
int y;
public:
Test(int a, int b)
{
x = a;
y = b;
}
void changeXY() const
{
x = 5;
//y = 7; This line produce an error
}
void display() const
{
// Add your own code
}
};
void main()
{
const Test obj(2,3);
obj.changeXY();
obj.display();
}
Saturday, May 29, 2010
Subscribe to:
Comments (Atom)