C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reference is not a member of class?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Oleg
Guest





PostPosted: Tue Jun 27, 2006 4:09 pm    Post subject: Reference is not a member of class? Reply with quote



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
}

Best,
Oleg Abrosimov.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Graham Menhennitt
Guest





PostPosted: Thu Jun 29, 2006 9:10 am    Post subject: Re: Reference is not a member of class? Reply with quote



On 27 Jun 2006 07:09:46 -0400, "Oleg" <beholder (AT) gorodok (DOT) net> wrote:

Quote:
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;
};

Think about it like this...

class test
{
public :
test(int& i) :
m_i(&i) {}

void f() const
{
*m_i = 0;
}

private :
int* m_i;
};

Would you expect that to give an error?

The const method is not changing anything that's a member of the
class. It's only changing something that's referenced via the class.
I'm sure somebody else will point you at the relevant chapter and
verse of the standard.

Graham

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Martin Bonner
Guest





PostPosted: Thu Jun 29, 2006 9:10 am    Post subject: Re: Reference is not a member of class? Reply with quote



Oleg wrote:
Quote:
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?

The logic is that you are not modifying the reference, you are
modifying the object that the reference refers to.

Quote:
class test
{
public :
test(int& i) : m_i(i) {}
void f() const { m_i = 0; }

private : int& m_i;
};


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
kanze
Guest





PostPosted: Thu Jun 29, 2006 9:10 am    Post subject: Re: Reference is not a member of class? Reply with quote

Oleg wrote:
Quote:
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 it doesn't modify the reference. It modifies what is
referred to. From the compiler's point of view, const is
bitwise; it affects the direct members of the class, and only
the direct members. In this case, the direct member is a
reference, which cannot be modified, const or not.

Experience has shown that from a design standpoint, logical
const is preferrable to bitwise const. But the compiler is
dumb; it doesn't know the actual abstractions you are
implementing, nor the logic behind the code. So we have mutable
and const_cast, for the occasional case where a bitwise
modification doesn't modify the logical value of an object, and
we depend on programmer discipline to ensure that const is
propagated correctly to the elements not covered by bitwise
const.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Gerhard Menzl
Guest





PostPosted: Thu Jun 29, 2006 9:10 am    Post subject: Re: Reference is not a member of class? Reply with quote

Oleg wrote:

Quote:
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! ]
Back to top
Maxim Yegorushkin
Guest





PostPosted: Thu Jun 29, 2006 9:10 am    Post subject: Re: Reference is not a member of class? Reply with quote

Oleg wrote:
Quote:
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.

Quote:
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! ]
Back to top
Francis Glassborow
Guest





PostPosted: Thu Jun 29, 2006 7:32 pm    Post subject: Re: Reference is not a member of class? Reply with quote

In article <1151312059.889096.283630 (AT) i40g2000cwc (DOT) googlegroups.com>, Oleg
<beholder (AT) gorodok (DOT) net> writes
Quote:
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
}
There is nothing wrong with that code, but there is in your

understanding of what const qualification does. It is perhaps easier to
understand with this code that uses pointers:
class test {
public :
test(int& i) :
m_i(i) {}
void f() const { * m_i = 0; } // OK do attempt to
change the pointer, only the object it is pointing to
void g(int * i_ptr) const { * m_i = i_ptr; } //
illegal
private :
int& m_i;
};




const qualification of an instance of a class type in C++ is shallow it
behaves as if you appended 'const' to each of the declared instance data
members. Of course in the case of references it is not syntactically
allowed because the semantics of references already prohibits changing
what the reference is but not the object referenced (unless you declare
it as a const reference)
--
Francis Glassborow ACCU
Author of 'You Can Do It!' and "You Can Program in C++"
see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/
youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.