overloading members on constness Problem

Link:
http://www.cplusplus.com/doc/tutorial/templates/
Part:Const member functions

Why bar.get() = 25; did not work although I used
int get() const {return x;}
NOT
const int& get() const {return x;}
??

Code:
// overloading members on constness
#include "stdafx.h"
#include <iostream>
using namespace std;

class MyClass {
int x;
public:
MyClass(int val) : x(val) {}
int get() const {return x;}
int& get() {return x;}
};

int _tmain(int argc, _TCHAR* argv[])
{
int c;
MyClass foo (10);
const MyClass bar (20);
foo.get() = 15; // ok: get() returns int&
bar.get() = 25; // I expect valid: get() returns int
cout << foo.get() << '\n';
cout << bar.get() << '\n';
cin>>c;
return 0;
}

Thank you.
Last edited on
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 =
int &get() const {return x;} did not work

error C2440: 'return' : cannot convert from 'const int' to 'int &' .
You are missing the point. I think you should read it(coder777's post) again. http://www.cplusplus.com/forum/general/141227/#msg745872

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// overloading members on constness
#include <iostream>
using namespace std;

class MyClass {
    int x;
  public:
    MyClass(int val) : x(val) {}
    const int& get() const {return x;}
    int& get() {return x;}
};

int main() {
  MyClass foo (10);
  const MyClass bar (20);
  foo.get() = 15;         // ok: get() returns int&
// bar.get() = 25;        // not valid: get() returns const int&
  cout << foo.get() << '\n';
  cout << bar.get() << '\n';

  return 0;
}


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.
Last edited on
Thank you for your reply.

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;}
Last edited on
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
But the program didn't work

How do you want it to 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.

Aceix.
Last edited on
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.
Topic archived. No new replies allowed.