Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Question on Myers #30 (http://www.velocityreviews.com/forums/t289138-question-on-myers-30-a.html)

Master of C++ 02-26-2005 05:33 AM

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.


Alf P. Steinbach 02-26-2005 05:43 AM

Re: Question on Myers #30
 
* Master of C++:
>
> 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?

Ioannis Vranos 02-26-2005 08:27 AM

Re: Question on Myers #30
 
Alf P. Steinbach wrote:

> 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

Ioannis Vranos 02-26-2005 08:39 AM

Re: Question on Myers #30
 
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.




--
Ioannis Vranos

http://www23.brinkster.com/noicys

Alf P. Steinbach 02-26-2005 11:16 AM

Re: Question on Myers #30
 
* Ioannis Vranos:
> 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?

White Wolf 02-26-2005 11:26 AM

Re: Question on Myers #30
 
Alf P. Steinbach wrote:
[SNIP]
> 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.



Ioannis Vranos 02-26-2005 12:38 PM

Re: Question on Myers #30
 
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.


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

Alf P. Steinbach 02-26-2005 12:57 PM

Re: Question on Myers #30
 
* Ioannis Vranos:
> 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.


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


> 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?

Ioannis Vranos 02-26-2005 03:22 PM

Re: Question on Myers #30
 
Alf P. Steinbach wrote:

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

Master of C++ 02-26-2005 03:32 PM

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.



All times are GMT. The time now is 07:40 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.