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?
>
> 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
> }
Because const in C++ is shallow, i.e. it does not propagate
automatically across levels of indirection. If m_i were a pointer, you
could modify the int being pointed to, but you could not reset the
pointer in test::f(). References cannot be reseated anyway, so the
constness of the containing object is redundant in this case.
--
Gerhard Menzl
#dogma int main ()
Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".
The information contained in this e-mail message is privileged and
confidential and is for the exclusive use of the addressee. The person
who receives this message and who is not the addressee, one of his
employees or an agent entitled to hand it over to the addressee, is
informed that he may not use, disclose or reproduce the contents thereof.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]