T
Tim H
How come this behaves the way it does? val_D() and val_E() cause a
(correct) compiler error. val_F() and val_G() do not, but I don't see
why.
Can anyone enlighten me?
#include <iostream>
using namespace std;
class foo {
public:
typedef int value_type;
typedef value_type& reference;
typedef const reference const_reference;
explicit foo(int x): m_val(x) {}
int val() { return m_val; }
int& val_A() { return m_val; }
value_type& val_B() { return m_val; }
reference val_C() { return m_val; }
const int& val_D() { return m_val; }
const value_type& val_E() { return m_val; }
const reference val_F() { return m_val; }
const_reference val_G() { return m_val; }
private:
int m_val;
};
int main()
{
foo x(0);
cout << x.val() << endl;
x.val_A() = 1;
x.val_B() = 2;
x.val_C() = 3;
x.val_D() = 4; // compile error
x.val_E() = 5; // compile error
x.val_F() = 6;
x.val_G() = 7;
cout << x.val() << endl;
return 0;
}
(correct) compiler error. val_F() and val_G() do not, but I don't see
why.
Can anyone enlighten me?
#include <iostream>
using namespace std;
class foo {
public:
typedef int value_type;
typedef value_type& reference;
typedef const reference const_reference;
explicit foo(int x): m_val(x) {}
int val() { return m_val; }
int& val_A() { return m_val; }
value_type& val_B() { return m_val; }
reference val_C() { return m_val; }
const int& val_D() { return m_val; }
const value_type& val_E() { return m_val; }
const reference val_F() { return m_val; }
const_reference val_G() { return m_val; }
private:
int m_val;
};
int main()
{
foo x(0);
cout << x.val() << endl;
x.val_A() = 1;
x.val_B() = 2;
x.val_C() = 3;
x.val_D() = 4; // compile error
x.val_E() = 5; // compile error
x.val_F() = 6;
x.val_G() = 7;
cout << x.val() << endl;
return 0;
}