Topic: Is 'friend friend' legitimate?


Author: "Hankel O'Fung" <hkfung@ust.hk>
Date: 1998/04/20
Raw View
Dear all,

Some people used to declare class members in the following order:
public, protected, private. Many of them also declare friends BEFORE
public members:

class foo {
      friend class enemy;      // declare friendship here
public:
      //...
protected:
      //...
private:
      //...
};

For some reasons, one may wish to put the friends between the public and
protected stuff, i.e.,

class foo {
public:
      //...
//friends:
      friend class enemy;      // declare friendship here
protected:
      //...
};

To be more extreme, one may, from an aesthetic point of view, want to
eliminate the use of comment '//' before 'friends:' (because for some
compilers, '//friends' may appear in a different colour to 'public:').
But 'friends' is not a keyword. So the natural way to work around is as
follows:

01   class foo {
02   public:
03         //...
04   friend
05         friend class enemy;      // declare 'friend friend class
enemy' here
06         template <class T> friend void bar::enemy();
07   protected:
08         //...
09   };

The question remains
(i) whether this 'friend friend' usage is legitimate,
(ii) if such usage really represents the intended (strange) use, and
(iii) how bad/good this programming style is.
Any idea?

Hankel
--
Dept of Ind Engg & Engg Mgt (IEEM)
HKUST, Clear Water Bay, Kowloon
Hong Kong

tel   (852) 2358 7103      fax   (852) 2358 0062
http://home.ust.hk/~hkfung

"For as long as I can remember, I've been searching for some reason
why we're here -- what are we doing here, who are we? If this is a
chance to find out even just a little part of that answer, I think
it's worth a human life, don't you?"
                                                   - Ellie Arroway
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Marc Girod <girod@stybba.ntc.nokia.com>
Date: 1998/04/21
Raw View
>>>>> "HOF" == Hankel O'Fung <hkfung@ust.hk> writes:

HOF> 01   class foo {
HOF> 02   public:
HOF> 03         //...
HOF> 04   friend
HOF> 05         friend class enemy;      // declare 'friend friend class
HOF> enemy' here
HOF> 06         template <class T> friend void bar::enemy();
HOF> 07   protected:
HOF> 08         //...
HOF> 09   };

Sorry, I don't directly answer your question, and I must say your
suggestion as such is not according to my taste.

However, I feel I agree with your goal. Here is what I do:

class foo {
  friend class enemy;
    template <class T> friend void bar::enemy();
  public:
    //...
  protected:
    //...
};

I.e.:
- I handle friend specifications as access control, with the same
  inlining  policy.
- In a class defining friends, I usually don't have any public part,
  so that the ordering problem doesn't show. Anyway, friends ideally
  characterize my external interface.
- I rely on inlining for visual support of scopes, so that I have
  these nested indentation levels. Access control groups are some kind
  of scope (on an abstract level)
- I am using GNU emacs, and within it cc-mode and font-lock. This
  allows me to support this scheme automatically and to color the
  keywords as I wish.

Best Regards!

--
Marc Girod                Valimo 1/2         Voice:  +358-9-511 63331
Nokia Telecommunications  P.O. Box 315       Mobile: +358-40-569 7954
NWS/NMS/NMS for Data      00045 NOKIA Group  Fax:    +358-9-511 63310
                          Finland            marc.girod@ntc.nokia.com





---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]