dubious use of friendship



 DEVELOP > c-Plus-Plus > dubious use of friendship

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 01 Jan 2008 07:39:18 AM
Object: dubious use of friendship
I saw some code where a struct is declared with all members public
(not explicitly so but by omission -- the word private never
appears). Then some operators are declared as friends of this
struct. Is there ever any reason to do this? Obviously, from a legal
and algorithmic standpoint, such code is redundant. However, I don't
feel ready to condemn it right away. For example, a future programmer
might want to declare some of the members private which would make the
friendship necessary. Furthermore, it signals that the named
operators will operate on the members of the struct which may be an
aid to understanding.
Any opinions?
Paul Epstein
.

User: "Victor Bazarov"

Title: Re: dubious use of friendship 01 Jan 2008 10:09:41 AM
wrote:

I saw some code where a struct is declared with all members public
(not explicitly so but by omission -- the word private never
appears). Then some operators are declared as friends of this
struct. Is there ever any reason to do this?

You mean, aside from not paying attention and just following the
habit of making non-member operators friends?

Obviously, from a legal
and algorithmic standpoint, such code is redundant. However, I don't
feel ready to condemn it right away. For example, a future programmer
might want to declare some of the members private which would make the
friendship necessary. Furthermore, it signals that the named
operators will operate on the members of the struct which may be an
aid to understanding.

Any opinions?

It is _conceivable_ that some code is written to be refactored later.
Like making something a struct and expecting to make it a class after
some testing/debugging is over. Is that a good idea? I do not think
so. But that's just my opinion.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: ""

Title: Re: dubious use of friendship 07 Jan 2008 06:01:01 AM
On Jan 2, 12:09=A0am, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:

pauldepst...@att.net wrote:

I saw some code where a struct is declared with all members public
(not explicitly so but by omission -- the word private never
appears). =A0Then some operators are declared as friends of this
struct. =A0Is there ever any reason to do this?


You mean, aside from not paying attention and just following the
habit of making non-member operators friends?

=A0Obviously, from a legal
and algorithmic standpoint, such code is redundant. =A0However, I don't
feel ready to condemn it right away. For example, a future programmer
might want to declare some of the members private which would make the
friendshipnecessary. =A0Furthermore, it signals that the named
operators will operate on the members of the struct which may be an
aid to understanding.


Any opinions?


It is _conceivable_ that some code is written to be refactored later.
Like making something a struct and expecting to make it a class after
some testing/debugging is over. =A0Is that a good idea? =A0I do not think
so. =A0But that's just my opinion.

V

I have found out the reason, and it seems that it was deliberate. The
practice here is use the keyword struct for a simple class with a few
data items but no methods. The keyword class is used for more
complicated classes. The coder made some operators friends of the
struct because he envisaged the struct becoming more complex, and
therefore (according to the above naming convention), being renamed a
class. The coder was ensuring that such a renaming would not result
in illegal code.
Paul Epstein
.
User: "Pete Becker"

Title: Re: dubious use of friendship 07 Jan 2008 07:30:52 AM
On 2008-01-07 07:01:01 -0500,
said:


I have found out the reason, and it seems that it was deliberate. The
practice here is use the keyword struct for a simple class with a few
data items but no methods. The keyword class is used for more
complicated classes. The coder made some operators friends of the
struct because he envisaged the struct becoming more complex, and
therefore (according to the above naming convention), being renamed a
class. The coder was ensuring that such a renaming would not result
in illegal code.

Seems like programmers today love to spend time solving non-problems.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
.



User: "Daniel T."

Title: Re: dubious use of friendship 01 Jan 2008 11:53:38 AM
wrote:

I saw some code where a struct is declared with all members public
(not explicitly so but by omission -- the word private never
appears). Then some operators are declared as friends of this
struct. Is there ever any reason to do this? Obviously, from a legal
and algorithmic standpoint, such code is redundant. However, I don't
feel ready to condemn it right away. For example, a future programmer
might want to declare some of the members private which would make the
friendship necessary. Furthermore, it signals that the named
operators will operate on the members of the struct which may be an
aid to understanding.

Any opinions?

I have to agree with Victor Bazarov. My guess is that the person who
wrote the code made the operators friends because that is what the
example code he was using had.
As to writing code so some future programmer might make it better. I'll
do that, but only if the "future" is in the next few hours, before I
check the code into CVS. (For example, when moving a member-variable
from one class to another, I will initially make the member-variable
public in its new home, fix all the compile errors, then start moving
parts of member-functions into the new home, then make the
member-variable private. Only after all the steps are done would I let
someone else look at the code. :-)
.
User: ""

Title: Re: dubious use of friendship 01 Jan 2008 06:58:09 PM
On Jan 2, 1:53=A0am, "Daniel T." <danie...@earthlink.net> wrote:

pauldepst...@att.net wrote:

I saw some code where a struct is declared with all members public
(not explicitly so but by omission -- the word private never
appears). =A0Then some operators are declared as friends of this
struct. =A0Is there ever any reason to do this? =A0Obviously, from a leg=

al

and algorithmic standpoint, such code is redundant. =A0However, I don't
feel ready to condemn it right away. For example, a future programmer
might want to declare some of the members private which would make the
friendship necessary. =A0Furthermore, it signals that the named
operators will operate on the members of the struct which may be an
aid to understanding.


Any opinions?


I have to agree with Victor Bazarov. My guess is that the person who
wrote the code made the operators friends because that is what the
example code he was using had.

As to writing code so some future programmer might make it better. I'll
do that, but only if the "future" is in the next few hours, before I
check the code into CVS. (For example, when moving a member-variable
from one class to another, I will initially make the member-variable
public in its new home, fix all the compile errors, then start moving
parts of member-functions into the new home, then make the
member-variable private. Only after all the steps are done would I let
someone else look at the code. :-)

Thanks to you and Victor for the feedback. Re "My guess is that the
person who

wrote the code made the operators friends because that is what the
example code he was using had."

I happen to know the programmer and I would make a different guess. I
think Victor was right to suggest he was making non-member operators
friends out of habit.
Paul Epstein
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER