 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Master of C++ Guest
|
Posted: Sat Feb 26, 2005 5:33 am Post subject: Question on Myers #30 |
|
|
Hi,
In Effective C++, #30, Myers says:
"Avoid member functions that return pointers or references to members
less accessible than themselves"
But, operator[] does exactly the opposite. Is overloading operator[]
considered "evil" in C++ circles ? I write a lot of numerical
programming code, and such operators make life easier for me.
Thanks,
Vijay.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Feb 26, 2005 5:43 am Post subject: Re: Question on Myers #30 |
|
|
* Master of C++:
| Quote: |
In Effective C++, #30, Myers says:
"Avoid member functions that return pointers or references to members
less accessible than themselves"
But, operator[] does exactly the opposite. Is overloading operator[]
considered "evil" in C++ circles ? I write a lot of numerical
programming code, and such operators make life easier for me.
|
"Effective C++" et.al. is just a compendium of rules-of-thumbs for
newbies. Very good for that, not very good when you understand the
issues and can apply intelligence rather then rote rules. The advice
cited above is, however, good for nothing... ;-)
A member function that returns a pointer or reference to a member
as accessible or more accessible than itself, is usually meaningless.
Perhaps the above is just a typo.
--
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?
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Sat Feb 26, 2005 8:27 am Post subject: Re: Question on Myers #30 |
|
|
Alf P. Steinbach wrote:
| Quote: | A member function that returns a pointer or reference to a member
as accessible or more accessible than itself, is usually meaningless.
Perhaps the above is just a typo.
|
I am not for the "usually" part. I am using many methods returning const
references, references or pointers to data members.
--
Ioannis Vranos
http://www23.brinkster.com/noicys
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Sat Feb 26, 2005 8:39 am Post subject: Re: Question on Myers #30 |
|
|
Ioannis Vranos wrote:
sure
| Quote: | for the "usually" part. I am using many
|
API
| Quote: | methods returning const
references, references or pointers to data members.
|
--
Ioannis Vranos
http://www23.brinkster.com/noicys
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Feb 26, 2005 11:16 am Post subject: Re: Question on Myers #30 |
|
|
* Ioannis Vranos:
| Quote: | Ioannis Vranos wrote:
I am not
sure
for the "usually" part. I am using many
API
methods returning const
references, references or pointers to data members.
|
Well, are you for example calling a public member function getHeight()
to obtain a reference to a public member mHeight, height_, myHeight?
Or a protected member function to obtain a reference to a protected
member, or a private one to obtain a reference to a private one?
That's what I think is usually very meaningless; the protection level of the
data member is then IMO not strict enough (which except for the "usually"
excludes private member function returning reference to private member,
but there might be a case for a virtual such).
I may be wrong though.
Perhaps if someone could quote or describe Myers' rationale?
--
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?
|
|
| Back to top |
|
 |
White Wolf Guest
|
Posted: Sat Feb 26, 2005 11:26 am Post subject: Re: Question on Myers #30 |
|
|
Alf P. Steinbach wrote:
[SNIP]
| Quote: | Perhaps if someone could quote or describe Myers' rationale?
|
It's Meyers. Scott Meyers. Shaken, not stirred. ;-)
--
WW aka Attila
:::
There are no passengers on spaceship Earth - we are all the crew.
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Sat Feb 26, 2005 12:38 pm Post subject: Re: Question on Myers #30 |
|
|
Alf P. Steinbach wrote:
| Quote: | Well, are you for example calling a public member function getHeight()
to obtain a reference to a public member mHeight, height_, myHeight?
|
Actually yes, this one in the form of (system-dependent) properties
implemented as getheight() and setheight() methods, which are implicitly
called.
But let's stick in another portable example:
vector's operator[] doesn't return a reference? Or begin() an iterator?
Isn't the following style common?
class SomeClass
{
string name;
// ...
public:
const string &getname() const { return name; }
void setname(const string &newname) { name= newname; }
// ...
};
--
Ioannis Vranos
http://www23.brinkster.com/noicys
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Sat Feb 26, 2005 12:57 pm Post subject: Re: Question on Myers #30 |
|
|
* Ioannis Vranos:
| Quote: | Alf P. Steinbach wrote:
Well, are you for example calling a public member function getHeight()
to obtain a reference to a public member mHeight, height_, myHeight?
Actually yes, this one in the form of (system-dependent) properties
implemented as getheight() and setheight() methods, which are implicitly
called.
|
Hm, not sure what you mean.
| Quote: | But let's stick in another portable example:
vector's operator[] doesn't return a reference? Or begin() an iterator?
|
They do, and the members they return references to are less accessible
(contrary to Meyers' advice).
| Quote: | Isn't the following style common?
class SomeClass
{
string name;
// ...
public:
const string &getname() const { return name; }
void setname(const string &newname) { name= newname; }
// ...
};
|
Yep, and again, contrary to Meyers' advice. Although I'd return that
string by value and let the compiler to the optimization.
--
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?
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Sat Feb 26, 2005 3:22 pm Post subject: Re: Question on Myers #30 |
|
|
Alf P. Steinbach wrote:
| Quote: | Isn't the following style common?
class SomeClass
{
string name;
// ...
public:
const string &getname() const { return name; }
void setname(const string &newname) { name= newname; }
// ...
};
Yep, and again, contrary to Meyers' advice. Although I'd return that
string by value and let the compiler to the optimization.
|
If you return the string by value most chances are that you will have a
redundant call to the copy constructor of the string for the temporary,
with a full copy of all its members.
In strings and other containers with large size, this is a major
drawback. That's why we have references and pointers.
I am not sure what Meyers means with that quote, but only he can explain
what he means exactly. The bottom line is, one should use const
references and const pointers instead of redundant temporaries, as much
as he can.
Perhaps Meyers means non-const references and pointers specifically, and
not the const ones. That is, I could agree with this:
"Avoid member functions that return non-const pointers or references to
members less accessible than themselves".
--
Ioannis Vranos
http://www23.brinkster.com/noicys
|
|
| Back to top |
|
 |
Master of C++ Guest
|
Posted: Sat Feb 26, 2005 3:32 pm Post subject: Re: Question on Myers #30 |
|
|
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.
Its not a typo. I verified it before posting.
-Vijay.
|
|
| Back to top |
|
 |
tair.sabirgaliev@gmail.co Guest
|
Posted: Sat Feb 26, 2005 4:01 pm Post subject: Re: Question on Myers #30 |
|
|
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.
|
|
| Back to top |
|
 |
Phil Staite Guest
|
Posted: Sat Feb 26, 2005 8:21 pm Post subject: Re: Question on Myers #30 |
|
|
Master of C++ wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
Rances Guest
|
Posted: Sat Feb 26, 2005 8:29 pm Post subject: Re: Question on Myers #30 |
|
|
"Phil Staite" <phil (AT) nospam (DOT) com> wrote
| Quote: | 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.
|
|
| Back to top |
|
 |
Ioannis Vranos Guest
|
Posted: Sat Feb 26, 2005 8:47 pm Post subject: Re: Question on Myers #30 |
|
|
Rances wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
White Wolf Guest
|
Posted: Sun Feb 27, 2005 12:57 am Post subject: Re: Question on Myers #30 |
|
|
Master of C++ wrote:
| Quote: | 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.
| Quote: | 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.'
|
|
| Back to top |
|
 |
|
|
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
|
|