SV: [LC++]Is friend our friend?

Jarmo Paavilainen netletter at comder.com
Mon Sep 3 19:10:34 UTC 2001


Hi,

...
> I have got the impression, rightly or wrongly, that using the c++
> keyword "friend" is frowned upon as "not the object oriented way".

Maybe not, but then again, who wants to write pure OO. I mix C and C++
frequently and in my opinion it makes my code easier to read and maintain.

...
> My argument though, is that sometimes objects may share a _conceptual_
> relationship, even though they are not related via inheritance.
> There may be two objects that work together like a team.  They
> rely on each other for full functionality to be achieved.  As
> such they need each other's private information.  The "friend"
> keyword allows this to be achieved.

And *I think* thats what the "friend" keyword is created for.

> Is there anything wrong with this argument?  Are there good
> reasons why "friend" should be avoided?  I have heard it said

None that I know off. Except it might be considered (by someone) bad coding
style.

...
> What if you have two classes which naturally function as a
> team.  Why not connect them via friend?  What is the
> alternative design strategy which is supposedly better?

The alternative is to make all members "public", and thats _realy bad_.

No *I think* "friend" is your friend. It gives you _perfect_ control of who
sees what.

"friend", "goto"... and others are not bad. And if you want to use them, use
them. Its only a question of _opinion_ and style, not a question of right or
wrong.

// Jarmo




More information about the tuxCPProgramming mailing list