Topic: Const member function question
Author: Biju Thomas <b_thomas@ibm.net>
Date: 1999/05/22 Raw View
Jim Hyslop wrote:
>
> Unless I've missed something,
> nothing in the Standard prohibits you from calling a non-const member
> function on a non-const data member - even within a const member
> function.
>
Yes, it prohibits such cases if the data member is an object contained
directly in the outer object.
Consider the following case:
struct X {
void f ();
};
struct Y {
X aX;
X* pX;
void g () const
{
aX.f(); // #1, Illegal
pX->f(); // #2, Correct
}
};
It is easy to see that #1 involves modifying the object directly,
whereas #2 doesn't do that.
--
Biju Thomas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1999/05/23 Raw View
Jim Hyslop <jim.hyslop@leitch.com> wrote:
: In article <373e350b.0@nexus.comcen.com.au>,
: "Robert Youdan" <rob@event-horizonsXXNOSPAMXX.com.au> wrote:
: >
: > The compiler should not allow a non-const function to be called or one
: > that
: > modifies any member of the class from within a const function.
Ignoring mutable, I think the statement is correct but the intent as
indicated in the example is incorrect.
: Not quite - the compiler should not allow a call to a non-const function
: that is a member of the current class.
Agreed.
: Unless I've missed something,
: nothing in the Standard prohibits you from calling a non-const member
: function on a non-const data member - even within a const member
: function.
Within the const member function, this is a pointer to const which
makes all data members const.
: Looking again at the original code:
Which did not modify a data member. It modified something outside of
the object via a data member which was a const pointer to non-const
external object.
Interesting when an object contains a pointer to non-const self :)
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/05/21 Raw View
In article <373e350b.0@nexus.comcen.com.au>,
"Robert Youdan" <rob@event-horizonsXXNOSPAMXX.com.au> wrote:
>
> The compiler should not allow a non-const function to be called or one
> that
> modifies any member of the class from within a const function.
Not quite - the compiler should not allow a call to a non-const function
that is a member of the current class. Unless I've missed something,
nothing in the Standard prohibits you from calling a non-const member
function on a non-const data member - even within a const member
function.
Looking again at the original code:
[snip]
> :class SomeObject
> :{
> :public:
> :void ChangeObject();
> :private:
> :int i;
> :};
[snip]
> :class X
> :{
> :public:
> :X();
> :private:
> :SomeObject * a;
> :void MemberFunction() const;
> :};
> :
[snip]
> :void X::MemberFunction() const
> :{
> :a -> ChangeObject();
> :}
In X::MemberFunction, 'this' is const, but in SomeObject::ChangeObject,
'this' (which now refers to SomeObject) is not const. Therefore the
code is legal, no questions asked.
--
Jim
I ignore all email from recruitment agencies.
Please do not send me email with questions - post
here.
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: zeisel@lnzx16.vai.co.at (Zeisel Helmut)
Date: 1999/05/17 Raw View
In article <373E14B2.989C64F3@abraxis.com>, Edward Diener <eddielee@abraxis.com> writes:
>
>Can a const member function change an internal object through its
>pointer or reference ? The reference itself would obviously not be
>changed and the pointer value is not being changed within the const
>member function, although the referred to or pointed to object is being
>changed.
>
>Example:
>
...
>
>class X
>{
>public:
>X();
>private:
>SomeObject * a;
>void MemberFunction() const;
>};
>
...
>
>void X::MemberFunction() const
>{
>a -> ChangeObject();
>}
>
>Is this last member function validly declared as const ?
>
>
Maybe you obtain what you want when you change the type of "a"
to "pointer to const":
class X
{
...
private:
const SomeObject * a;
};
Helmut
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/05/18 Raw View
"Robert Youdan" <rob@event-horizonsXXNOSPAMXX.com.au> writes:
>Edward Diener wrote in message <373E14B2.989C64F3@abraxis.com>...
>:
>:Can a const member function change an internal object through its
>:pointer or reference ? The reference itself would obviously not be
>:changed and the pointer value is not being changed within the const
>:member function, although the referred to or pointed to object is being
>:changed.
>The compiler should not allow a non-const function to be called or one that
>modifies any member of the class from within a const function.
That's correct (the language defintion forbids such code), but
does not apply to this situation.
The situation is analagous to the following:
int i, j;
int* const p = &i;
p = &j; // error, p is const
*p = 2; // OK, *p is not const
If the class object is const, the members are treated as const.
You can't change its members unless they are declared "mutable" or
you use a cast. You can change the value of non-const things that
member pointers or references point to.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Edward Diener <eddielee@abraxis.com>
Date: 1999/05/16 Raw View
Can a const member function change an internal object through its
pointer or reference ? The reference itself would obviously not be
changed and the pointer value is not being changed within the const
member function, although the referred to or pointed to object is being
changed.
Example:
class SomeObject
{
public:
void ChangeObject();
private:
int i;
};
void SomeObject::ChangeObject()
{
i = 1;
}
class X
{
public:
X();
private:
SomeObject * a;
void MemberFunction() const;
};
X::X()
{
a = new SomeObject;
}
void X::MemberFunction() const
{
a -> ChangeObject();
}
Is this last member function validly declared as const ?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/05/16 Raw View
On 16 May 1999 00:50:41 GMT, Edward Diener <eddielee@abraxis.com> wrote:
>Can a const member function change an internal object through its
>pointer or reference ? The reference itself would obviously not be
>changed and the pointer value is not being changed within the const
>member function, although the referred to or pointed to object is being
>changed.
This is correct, and your program is correct.
But your program is not correct. You need a copy constructor,
destructor, and operator= for your class X (the one that contains a
pointer to a newly created object).
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Joerg Barfurth <jbarfurth@vossnet.de>
Date: 1999/05/16 Raw View
Edward Diener wrote:
>
> Can a const member function change an internal object through its
> pointer or reference ? The reference itself would obviously not be
> changed and the pointer value is not being changed within the const
> member function, although the referred to or pointed to object is being
> changed.
[snip]
>
> void X::MemberFunction() const
> {
> a -> ChangeObject();
> }
>
> Is this last member function validly declared as const ?
Yes. Using 'mutable' you get get the possibility to do the same even for
direct class members.
It is up to you to define what it means for an object of your class to be const.
It should mean that it's _externally_ visible state desn't change.
If the state of another object referred to by the object isn't part of it's
state, or if any such changes don't change the state of your object, this can
be valid design. (e.g. the other object might log accesses to the first).
Another example are smart pointer classes: The pointer doesn't change, while
the object pointed to may.
-- J rg Barfurth
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Robert Youdan" <rob@event-horizonsXXNOSPAMXX.com.au>
Date: 1999/05/16 Raw View
The compiler should not allow a non-const function to be called or one that
modifies any member of the class from within a const function. Some,
however, might issue a warning but allow you to continue in an effort to get
some 1/2 thought out code to run.
Rob
Edward Diener wrote in message <373E14B2.989C64F3@abraxis.com>...
:
:Can a const member function change an internal object through its
:pointer or reference ? The reference itself would obviously not be
:changed and the pointer value is not being changed within the const
:member function, although the referred to or pointed to object is being
:changed.
:
:Example:
:
:class SomeObject
:{
:public:
:void ChangeObject();
:private:
:int i;
:};
:
:void SomeObject::ChangeObject()
:{
:i = 1;
:}
:
:class X
:{
:public:
:X();
:private:
:SomeObject * a;
:void MemberFunction() const;
:};
:
:X::X()
:{
:a = new SomeObject;
:}
:
:void X::MemberFunction() const
:{
:a -> ChangeObject();
:}
:
:Is this last member function validly declared as const ?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]