C++ friend functions

Discussion in 'C++' started by coder12, Sep 15, 2012.

  1. coder12

    coder12 New Member

    Joined:
    Sep 15, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    can 2 classes have one common friend function?if yes,then how can it be accessed from the classes?(the friend function is defined outside of both the classes)
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Moved to C++ Forum
     
  3. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    Yes,

    A friend function can access the private or publilc members of a class as it were members of those classes.

    I would represent the following example though it is not :worried:exhaustive.
    Code:
         #inclulde <iostream>
      #include <stdlib.h>
       using namespace std;
       class Value
      {
       private:
          int  m_nValue;
       public:
        Value(int nValue) {m_nValue=nVallue;}
       friend bool IsEqual(const Value &cValue1, const Value &cValue2);
    };
    {
      return (Value1.m_nValue==cValue2.m_nValue);
    }
     
    Last edited by a moderator: Apr 21, 2014

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice