473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
Jan 1 '08 #1
5 1244
pa**********@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). 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
Jan 1 '08 #2
pa**********@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). 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. :-)
Jan 1 '08 #3
On Jan 2, 1:53*am, "Daniel T." <danie...@earthlink.netwrote:
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). *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. :-)
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
Jan 2 '08 #4
On Jan 2, 12:09*am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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). *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
friendshipnecessary. *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
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

Jan 7 '08 #5
On 2008-01-07 07:01:01 -0500, pa**********@att.net 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)

Jan 7 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Marcin Vorbrodt | last post by:
Is friendship inherited? Meaning if: class Base { public: friend FriendClass; }; class Derived : public Base { }
7
by: Erik | last post by:
private and protected members can only be accessed by deriving a class or by giving friendship. The problem with friendship is that friend statements are written inside the class. A FriendShip...
4
by: JH Trauntvein | last post by:
Consider the following example: namespace n1 { class cn1_base; namespace n1_helpers { class helper1 {
4
by: Mr Dyl | last post by:
I'm trying to declare the following friendship and VS.Net 2003 is complaining: template <class T> class Outter { class Inner {...} ... }
3
by: tom.s.smith | last post by:
I know that friendship isn't inherited in C++, and have two questions. (1) Why? Is it a technical problem, or just by design? If the latter, isn't this a little prescriptive, and shouldn't there...
2
by: Mark P | last post by:
Is there a way to do anything like the following: class A { friend void B::foo(); ... }; class B
4
by: werasm | last post by:
I've read the following somewhere: "Friendship is the strongest form of coupling there is. That is, you introduce a high degree of dependency when you declare a friend, since the friend becomes...
6
by: Hicham Mouline | last post by:
Hello, A semi-skeptical colleague is asking me why friendship is not inheritable, specifically: class Base { public: virtual void f() const =0; }; class Derived : public Base {
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.