Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question on Myers #30

Reply
Thread Tools

Question on Myers #30

 
 
tair.sabirgaliev@gmail.com
Guest
Posts: n/a
 
      02-26-2005
if you are overloading operator[] for example for Array class, it is
not contrary to Myers, because Myers said that for general design, but
I think CONTAINER classes are exception to this rule.

 
Reply With Quote
 
 
 
 
Phil Staite
Guest
Posts: n/a
 
      02-26-2005
Master of C++ wrote:
> Hi,
>
> In Effective C++, #30, Myers says:
>
> "Avoid member functions that return pointers or references to members
> less accessible than themselves"


Seems straightforward to me. IMHO if you give out a pointer or
reference to any data member you're violating encapsulation. (but yes,
sometimes it has to be done) As Mr. Meyers points out, giving out
pointers or refs to data members with more restricted access (eg. a
public member function returning a pointer to a private member) is even
worse. Heck, you might as well just make the member public and call it
a struct...

Additionally, just the fact that you're returning pointers/refs to data
members is suspect. Generally that means your data is in one place or
class, and the code that operates on it is in another. Not good for
encapsulation. And yes, I know there are many many exceptions to this
idea and instances where you just "have to" expose data/members in some
ways. I'm just saying if you're doing that, make sure you're doing it
for a good/valid reason.
 
Reply With Quote
 
 
 
 
Rances
Guest
Posts: n/a
 
      02-26-2005

"Phil Staite" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Master of C++ wrote:
>> Hi,
>>
>> In Effective C++, #30, Myers says:
>>
>> "Avoid member functions that return pointers or references to members
>> less accessible than themselves"

>
> Seems straightforward to me. IMHO if you give out a pointer or reference
> to any data member you're violating encapsulation. (but yes, sometimes it
> has to be done)


PUblic functions returning const references to large private data members
such as vectors is a pretty good design pattern IMO. You can use the
reference in a const manner very efficiently, or use an inneficient copy in
a non-const manner.


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      02-26-2005
Rances wrote:

> PUblic functions returning const references to large private data members
> such as vectors is a pretty good design pattern IMO. You can use the
> reference in a const manner very efficiently, or use an inneficient copy in
> a non-const manner.



Also, a return of a const reference or const pointer to something does
not necessarily expose the internal data of a class, although in most
times it is internal data of the class, however it may be not, and thus
it is not against data hiding.


Bottom line is, returning a redundant string temporary which is a copy
of an internal string member does not provide any more data hiding than
a const reference or pointer to the same string member.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
White Wolf
Guest
Posts: n/a
 
      02-27-2005
Master of C++ wrote:
> Of course, I don't follow EC++ as if it were a rule-book. But a lot of
> well-respected authors (Herb Sutter, Steve McConnell etc.,) cite Myers
> heavily, and I have respect for what he says in his book. Thats what
> makes this advice a little confusing.


No, they don't.

> Its not a typo. I verified it before posting.


It is a typo. The book is written by Scott Meyers.

--
WW aka Attila
:::
A red sign on the door of a physics professor: 'If this sign is blue,
you're going too fast.'


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-27-2005
* Ioannis Vranos:
> Rances wrote:
>
> > PUblic functions returning const references to large private data members
> > such as vectors is a pretty good design pattern IMO. You can use the
> > reference in a const manner very efficiently, or use an inneficient copy in
> > a non-const manner.

>
>
> Also, a return of a const reference or const pointer to something does
> not necessarily expose the internal data of a class, although in most
> times it is internal data of the class, however it may be not, and thus
> it is not against data hiding.


Agreed.


> Bottom line is, returning a redundant string temporary which is a copy
> of an internal string member does not provide any more data hiding than
> a const reference or pointer to the same string member.


Well, the reference actually _is_ an exposure. A common case where this may
bite you (at least it tried to gnaw some meat off my leg, I just
kicked it very hard and never seen it since) is where you want to change the
internal implementation so you no longer have a member. It's probably a
purely philosophical question where the blame really is, then: the reference
result, versioning, ill-behaved client code, what, but in practice problems
can be avoided simply by avoiding the reference result in the first place. As
I see it it's definitely a case of Knuth's evil premature optimization.
Keeping one's set of options open is, after all, the favorite strategy of good
programmers, who thereby are lazy by definition, I find that interesting....

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      02-27-2005
Alf P. Steinbach wrote:

> Well, the reference actually _is_ an exposure. A common case where this may
> bite you (at least it tried to gnaw some meat off my leg, I just
> kicked it very hard and never seen it since) is where you want to change the
> internal implementation so you no longer have a member. It's probably a
> purely philosophical question where the blame really is, then: the reference
> result, versioning, ill-behaved client code, what, but in practice problems
> can be avoided simply by avoiding the reference result in the first place. As
> I see it it's definitely a case of Knuth's evil premature optimization.
> Keeping one's set of options open is, after all, the favorite strategy of good
> programmers, who thereby are lazy by definition, I find that interesting....



If you want to keep the old programming interface of your class, you can
easily change const string & to const string.


If you were returning a non-const reference(!) to your internal data
member, then you shouldn't do it.


What Meyers is saying is absolutely right for non-const references, and
my guess is that it is what he means.



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-27-2005
* Ioannis Vranos:
>
> If you want to keep the old programming interface of your class, you can
> easily change const string & to const string.


Not without potentially breaking client code.


> If you were returning a non-const reference(!) to your internal data
> member, then you shouldn't do it.


Right.


> What Meyers is saying is absolutely right for non-const references, and
> my guess is that it is what he means.


That doesn't seem to follow.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      02-27-2005
Alf P. Steinbach wrote:

>>If you want to keep the old programming interface of your class, you can
>>easily change const string & to const string.

>
>
> Not without potentially breaking client code.



May you provide an example?



--
Ioannis Vranos

http://www23.brinkster.com/noicys
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-27-2005
* Ioannis Vranos:
> Alf P. Steinbach wrote:
>
> >>If you want to keep the old programming interface of your class, you can
> >>easily change const string & to const string.

> >
> >
> > Not without potentially breaking client code.

>
>
> May you provide an example?



void client( bool thisOrThat )
{
std::string const gwb = "Bah!";
std::string const* ps = thisOrThat? &gwb : &iv.getstring();

std::cout << *ps << std::endl;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Kanye West show was Mike Myers SECOND awkward live tv moment heinzmenses@hotmail.com Digital Photography 31 09-12-2005 08:54 PM
Michael Myers A+ 5th edition question Fred Mathering A+ Certification 4 05-27-2004 04:46 AM
Mike Myers 70-293 Book Brian MCSA 2 08-04-2003 01:10 PM
Mike Myers Book? steven A+ Certification 2 08-04-2003 02:02 AM
Re: A+!! Thanks M. Myers techophile A+ Certification 1 06-24-2003 07:43 PM



Advertisments