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 

Re: Const Correctness

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





PostPosted: Fri Aug 29, 2003 8:28 am    Post subject: Re: Const Correctness Reply with quote



In article <fc9d89cd.0308271942.14dad9ee (AT) posting (DOT) google.com>, Eduardo
Bezerra <edubez (AT) visualnet (DOT) com.br> writes
Quote:
Suppose that I have this:

/////////////////////////////////////////////
typedef vector<int> IntArray;

class A {
public:
IntArray cont;
};

class B {
private:
A* p_;
public:
B() : p_(new A) {}
~B() {delete p_;}
IntArray& Func() const {return p_->cont;}
};
/////////////////////////////////////////////

Should B::Func be const or not ?

In so far as Func does not change the state of a B object make it const.
However I think such designs completely miss the point of having data
private. We would have to see much more of the design to pass any
sensible judgement. As it stands code such as:

int main(){
B b1, b2;
b1 = b2;
}

is totally flawed. That leads me to ask why p_ is not an A * const.


--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


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

Back to top
Richard Smith
Guest





PostPosted: Fri Aug 29, 2003 8:33 am    Post subject: Re: Const Correctness Reply with quote



Eduardo Bezerra wrote:

Quote:
typedef vector<int> IntArray;

class A {
public:
IntArray cont;
};

class B {
private:
A* p_;
public:
B() : p_(new A) {}
~B() {delete p_;}
IntArray& Func() const {return p_->cont;}
};

I hope you're going to do something about the default
generated copy constructor and assignment operator.
Consider

int main() {
B b1;
B b2(b1); // b2 holds same A as b1

} // Ooops. double destruction of A.


Quote:
/////////////////////////////////////////////

Should B::Func be const or not ?

It depends. From the point of making the code compile, it
can be, but needn't be. The real answer depends on what
the relationship between B and A represents. If A is very
much an implementation detail of B (e.g. a pImpl), then
propogating the constness makes sense -- i.e. one of the
following:

IntArray& B::Func() { return p_->cont; }
IntArray const& B::Func() const { return p_->cont; }

If semantically, p_ is a handle to some additional
component, then not propogating the constness is better
(i.e. as you've done).


If you're still not sure, ask yourself whether modifying
p_->cont affects the semantic state of the B object. If it
does, then propogate constness (i.e. make the method
non-const), it not then make the method const.

--
Richard Smith

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

Back to top
Joshua Lehrer
Guest





PostPosted: Fri Aug 29, 2003 8:45 am    Post subject: Re: Const Correctness Reply with quote



[email]edubez (AT) visualnet (DOT) com.br[/email] (Eduardo Bezerra) wrote in message news:<fc9d89cd.0308271942.14dad9ee (AT) posting (DOT) google.com>...
Quote:

Should B::Func be const or not ?

Best regards,
Eduardo


Scott Meyers discusses this in "Effective C++"

It is covered in a few of the following topics. I highly recommend
you read this section, if not the entire book.

5. Classes and Functions: Design and Declaration.

Strive for class interfaces that are complete and minimal.
Differentiate among member functions, non-member functions, and friend
functions.
Avoid data members in the public interface.
Use const whenever possible.
Prefer pass-by-reference to pass-by-value.
Don't try to return a reference when you must return an object.
Choose carefully between function overloading and parameter
defaulting.
Avoid overloading on a pointer and a numerical type.
Guard against potential ambiguity.
Explicitly disallow use of implicitly generated member functions you
don't want.
Partition the global namespace.


joshua lehrer
factset research systems
NYSE:FDS

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

Back to top
Andrey Tarasevich
Guest





PostPosted: Fri Aug 29, 2003 10:46 am    Post subject: Re: Const Correctness Reply with quote

Eduardo Bezerra wrote:
Quote:
...
Suppose that I have this:

/////////////////////////////////////////////
typedef vector<int> IntArray;

class A {
public:
IntArray cont;
};

class B {
private:
A* p_;
public:
B() : p_(new A) {}
~B() {delete p_;}
IntArray& Func() const {return p_->cont;}
};
/////////////////////////////////////////////

Should B::Func be const or not ?
...

There is absolutely no way to answer this question without knowing more
about your design intent. No one knows the answer, except you.

What design concept are you trying to implement by storing a pointer to
'A' inside 'B'? Is the object of type 'A' pointed by 'B::p' considered
to be an integral part of object of class 'B'? In other words, in your
design does 'B' _aggregate_ an instance of 'A' or simply refer to a
completely _independent_ instance of 'A'? In the former case
(aggregation) 'B::Func' should not be 'const', because the returned
reference is not constant. In the latter case (object reference) it
might make sense to declare it as 'const'.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


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

Back to top
Siemel Naran
Guest





PostPosted: Fri Aug 29, 2003 11:10 am    Post subject: Re: Const Correctness Reply with quote

"Eduardo Bezerra" <edubez (AT) visualnet (DOT) com.br> wrote in message

Quote:
class A {
public:
IntArray cont;
};

class B {
private:
A* p_;
public:
B() : p_(new A) {}
~B() {delete p_;}
IntArray& Func() const {return p_->cont;}
};

Should B::Func be const or not ?

It should most likely be non-const because you return a non-const reference.
If you returned a const IntArray&, then yes the function should be const.

--
+++++++++++
Siemel Naran


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

Back to top
Ulrich Eckhardt
Guest





PostPosted: Fri Aug 29, 2003 11:29 am    Post subject: Re: Const Correctness Reply with quote

Eduardo Bezerra wrote:
[class B with pointer to other object]
[Func() returning *pointer as non-const ref]
Quote:
Should B::Func be const or not ?

Constantness doesn't propagate through pointers. To be precise, inside a
const memberfunction, the pointer is constant but this doesn't apply to
the object it points to.

Uli

--
Questions ?
see C++-FAQ Lite: http://parashift.com/c++-faq-lite/ first !


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

Back to top
Andrey Tarasevich
Guest





PostPosted: Sat Aug 30, 2003 1:15 pm    Post subject: Re: Const Correctness Reply with quote

White Wolf wrote:
Quote:
Eduardo Bezerra wrote:
Hi,

Suppose that I have this:

/////////////////////////////////////////////
typedef vector<int> IntArray;

class A {
public:
IntArray cont;
};

class B {
private:
A* p_;
public:
B() : p_(new A) {}
~B() {delete p_;}
IntArray& Func() const {return p_->cont;}
};
/////////////////////////////////////////////

Should B::Func be const or not ?

IMHO it should not be there. Sad Why do you hide this array in a class if
finally you make it all available by a "getter" function?


Your are asking a question that doesn't exist. You are provided with a
getter function that returns a reference to an array. Where is this
array hidden - you don't know and are not supposed to know, since it is
an implementation detail and none of your business. Just because you was
naughty enough to peek into the class' implementation details doesn't
mean that you have a right to ask this question :)

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


[ 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.