declaring friend

Discussion in 'C++' started by Tony Johansson, Aug 15, 2005.

  1. Hello experts!

    Assume I have this class definition of class ListElem.
    I'm a bit unsure how to interpret when you put friend declaration in public,
    protected and private section of a class definition. If you insted have
    declared a primitive type or a class type then I would understand it
    completely.
    I assume that having a friend declaration in the private section make no
    sense.
    I assume that you always put friend declaration in the public section.

    Example 1
    class ListElem
    {
    public:
    friend class List
    .. . .
    };

    Example 2
    class ListElem
    {
    protected:
    friend class List
    .. . .
    };

    Exampl 3
    class ListElem
    {
    private:
    friend class List
    .. . .
    };

    Many thanks

    //Tony
     
    Tony Johansson, Aug 15, 2005
    #1
    1. Advertising

  2. Tony Johansson

    benben Guest

    "Tony Johansson" <> wrote in message
    news:4eYLe.144010$...
    > Hello experts!
    >
    > Assume I have this class definition of class ListElem.
    > I'm a bit unsure how to interpret when you put friend declaration in
    > public, protected and private section of a class definition. If you insted
    > have declared a primitive type or a class type then I would understand it
    > completely.


    No difference where you declare a friend in the class.

    > I assume that having a friend declaration in the private section make no
    > sense.
    > I assume that you always put friend declaration in the public section.


    By declaring something as a friend you are granting permission for that
    something to access the private part of the class. A sole friend statement
    encapsulates nothing and therefore is not bound by access permissions.
    Logically a friend (class or function) is an augmentation of a class's
    public interface, and it seems natural to put that in public. However,
    unlike member functions, you don't need to know the class to use its friend,
    so it is okay to put the friend statement in private.

    Ben
     
    benben, Aug 15, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Aguilar
    Replies:
    5
    Views:
    461
    Rob Williscroft
    Nov 29, 2004
  2. Replies:
    3
    Views:
    520
    Damien
    Aug 8, 2006
  3. dwelzel
    Replies:
    2
    Views:
    311
    dwelzel
    Jan 28, 2007
  4. Hicham Mouline
    Replies:
    0
    Views:
    338
    Hicham Mouline
    Nov 11, 2008
  5. Hicham Mouline
    Replies:
    1
    Views:
    356
    Vladyslav Lazarenko
    Mar 27, 2009
Loading...

Share This Page