(1) int get() const {return x;}
We have two advantages here,
1. const and non-const class object can call this function.
const A obj1;
A obj1;
obj1.get();
obj2.get();
2. this function will not modify the member variables for the class
class A
{
public:
int a;
A()
{
a=0;
}
int get() const
{
a=20; // error: increment of data-member 'A::a' in read-only structure
return x;
}
}
While changing the member variable of the class [a] by constant function, compiler throws error.
(2) const int& get() {return x;}
returning the pointer to constant integer reference.
(3)const int& get() const {return x;}
it is combination answer (2) and (3).