the "friend" function .... ?

This is a discussion on the "friend" function .... ? within the C++ Programming forums, part of the General Programming Boards category; Hi, I accidently came accross the friend function when I was initilizing a variable ... does anyone know what it ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262

    Smile the "friend" function .... ?

    Hi, I accidently came accross the friend function when I was initilizing a variable ... does anyone know what it does and how to use it?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If you want to give ( possibly unrelated ) classes or functions acces to protected members of a class you use the keyword friend.
    Commonly used for stream inserters/extractors.
    e.g

    Code:
    class foo{
    public: 
      foo():v(0) {}
      friend ostream & operator << ( ostream & str, foo & inst );
    protected:
      int v;
    };
    
    // without beeing friend to foo the stream inserter would not be allowed to access inst.v
    inline ostream & operator << ( ostream & str, foo & inst ) {
       return str << inst.v;
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    Generally used in classes to give functions or other classes access to the private data members

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21