Re: Reference is not a member of class?
"Oleg" <beholder@gorodok.net> writes:
This code compiles on Test Drive Comeau C++ Online and on VC
7.1. So, I assume that it is standard compliant. but why it is
allowed to modify non-const reference in const method?
On a conceptual level, because the value of the object refered to by
m_i doesn't contribute to the state of the test instance.
On the language level, because there is nothing in the Standard that
wouldn't allow it.
class test
{
public :
test(int& i) :
m_i(i) {}
void f() const
{
m_i = 0;
}
private :
int& m_i;
};
int main()
{
int i = 1;
const test t(i);
t.f(); // set i to 0
Which is ok. i isn't const after all.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]