Philosophical reason behind the concept of friend function

Discussion in 'C++' started by Madhav, Mar 13, 2006.

  1. Madhav

    Madhav Guest

    Hi all,
    I am a newbie in c++. I want to know what is the
    philosophical reason behind the existence of friend functions. I
    thought giving access to private data to a function which is not a
    member of the class is a violation of encapsulation.

    Thanks,
    Madhav.
    Madhav, Mar 13, 2006
    #1
    1. Advertising

  2. Madhav wrote:
    > Hi all,
    > I am a newbie in c++. I want to know what is the
    > philosophical reason behind the existence of friend functions. I
    > thought giving access to private data to a function which is not a
    > member of the class is a violation of encapsulation.
    >
    > Thanks,
    > Madhav.
    >


    Unfortunately, we're not living in an ideal world. That means that
    sometimes (like when building singleton holder or some generic
    framework) it's OK to access one's private properties. And, yeah,
    sometimes (hey, I said SOMETIMES, don't kill me!) it is even appropriate
    to use goto :eek:)
    Aleksander Beluga, Mar 13, 2006
    #2
    1. Advertising

  3. Madhav

    GB Guest

    Madhav wrote:
    > Hi all,
    > I am a newbie in c++. I want to know what is the
    > philosophical reason behind the existence of friend functions. I
    > thought giving access to private data to a function which is not a
    > member of the class is a violation of encapsulation.
    >
    > Thanks,
    > Madhav.
    >


    It is only giving access to *arbitrary* functions (e.g., by making data
    public) that is a violation of encapsulation. Having the class identify
    a *specific* function for access is not a violation of encapsulation,
    since the class is giving that access. The function becomes part of the
    encapsulation along with the member functions. That said, it should be
    rare that non-members need such access.

    Gregg
    GB, Mar 13, 2006
    #3
  4. Madhav

    benben Guest

    Madhav wrote:
    > Hi all,
    > I am a newbie in c++. I want to know what is the
    > philosophical reason behind the existence of friend functions. I
    > thought giving access to private data to a function which is not a
    > member of the class is a violation of encapsulation.
    >
    > Thanks,
    > Madhav.
    >


    The philosophical reason: to extend encapsulation boundary over that
    defined by the class.

    Ask yourself what is being encapsulated from whom :)

    Regards,
    Ben
    benben, Mar 13, 2006
    #4
  5. Madhav

    Jerry Coffin Guest

    In article <1142221918.688510.290930
    @i39g2000cwa.googlegroups.com>,
    says...
    > Hi all,
    > I am a newbie in c++. I want to know what is the
    > philosophical reason behind the existence of friend functions. I
    > thought giving access to private data to a function which is not a
    > member of the class is a violation of encapsulation.


    The reason is pretty simple: sometimes functions that are
    part of the interface to a class can't actually be member
    functions. When this happens, the non-member function is
    normally made a friend of the class.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Mar 13, 2006
    #5
    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. Dave Rahardja
    Replies:
    11
    Views:
    505
    Risto Lankinen
    Aug 7, 2003
  2. Maurice LING
    Replies:
    2
    Views:
    295
    Paul Rubin
    Jun 3, 2005
  3. Patricia Shanahan
    Replies:
    15
    Views:
    454
    Daniel Pitts
    Mar 6, 2007
  4. neel
    Replies:
    2
    Views:
    240
    Pavel
    Jun 14, 2009
  5. Good Guy
    Replies:
    1
    Views:
    249
    Andrea Crotti
    Nov 19, 2010
Loading...

Share This Page