Thread: class friendship.

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    class friendship.

    Hi, I'm learning the OOP of C++, and the book mention friendship function. From what I understand from the explanation and implementation in the book, friend function is used for operator overloading. So, my questions are:
    1. Besides the purpose of overloading the operators, isn't the friend function and the member function the same? By this, I mean that instead of create a friend function, could I just create a member function to do the same job.

    2. Is there other usage for the friend function?

    Thanks for all the inputs.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, well, friend is not a function; friend is a keyword that does what your book explains. It is true that most of the time, you will use the friend function to implement operator overloaded functions, but functions are not the sole use of friend. A class might be friends with another class.

    A member function could do everything a friend function does, as long as there is public access. For example you implement the operator << and there is a public data() function. If what data() returns is what you want to display, then there is no problem: you could easily implement the operator, using data().

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Gotcha. Thanks for the help, whiteflags.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM