int get() const {return x;} returns a so called rvalue. It can only be used on the right side of =
Since it's a copy of a value it makes no sense to assign something to it
int&get() const {return x;} returns a so called lvalue. It can be used on the left (and right) side of =
Member functions specified to be const cannot modify non-static data members nor call other non-const member functions. In essence, const members shall not modify the state of an object.
const objects are limited to access only members marked as const, but non-const objects are not restricted can access both const members and non-const members alike.
You may think that anyway you are seldom going to declare const objects, and thus marking all members that don't modify the object as const is not worth the effort, but const objects are actually very common. Most functions taking classes as parameters actually take them by const reference, and thus, these functions can only access their const members:
Umm..not sure why you reported me but anyways how do you expect to have a constant object but then modify a value of it? If you wish to return by reference you must return a constant reference.
so if function is const with const object,It doesn't matters whether
int &get() const {return x;}
or const int& get() const {return x;}
since it is not allowed for the function to return value?
second for the non const object foo why should the returned value be with reference int& get() {return x;}
It didn't work when I use int get() {return x;}
If the object is not constant then you should be allowed to modify the member that you get so you would return a reference. If it is a constant object then you should not be allowed to modify the member you get so it should be a constant reference.
I didn't use "const int& get() const {return x;}" which returns const .
I used "int &get() const {return x;}" where the method is const in order to be used by "bar" which is a constant class i.e overloading members on constness
But the program didn't work
1.The overloads of get would render one ambiguous, and so it is impossible. The overload must differ in parameter type.
2. Int& get() const should not work because, the const tag is promising us that the function would not change anything, but the return type is such that, an outsider can change a member variable, through the function.
1.The overloads of get would render one ambiguous, and so it is impossible. The overload must differ in parameter type.
>>as per http://www.cplusplus.com/doc/tutorial/templates/
the overloading is done according to constness ie const object use const members
2. Int& get() const should not work because, the const tag is promising us that the function would not change anything, but the return type is such that, an outsider can change a member variable, through the function.
>>according to your reply there is no difference bet.
const int& get() const {return x;}
and "int &get() const {return x;}
However the second one exist in the same link and can return value.