class friendship.

This is a discussion on class friendship. within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm learning the OOP of C++, and the book mention friendship function. From what I understand from the explanation ...

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

    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,763
    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
    460
    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, 09: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, 02:03 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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