7

In a class, if the function is declared as friend within the different specifier like - private, protected, or public, then is there any difference. As per my understanding, friend function is not a member. Thus, it shouldn't matter. But, if I see static - it is also not a member, but access specifier matters a lot. So, I am a bit confused. How all these code works fine? Is there any difference among the following classes?

/** Private friend function **/

class frienddemoFunction
{
  private:
      unsigned int m_fanSpeed;
      unsigned int m_dutyCycle;
      /** This function is not a member of class frienddemo **/
      friend void printValues(frienddemoFunction &d);

  public:
      void setFanSpeed(unsigned int fanSpeed);
      unsigned int getFanSpeed();

};


/** Protected -- Friend Function **/
class frienddemoFunction
{
  private:
      unsigned int m_fanSpeed;
      unsigned int m_dutyCycle;

  public:
      void setFanSpeed(unsigned int fanSpeed);
      unsigned int getFanSpeed();

 protected:

 /** This function is not a member of class frienddemo **/
      friend void printValues(frienddemoFunction &d);


};

class frienddemoFunction
{
  private:
      unsigned int m_fanSpeed;
      unsigned int m_dutyCycle;

  public:
      void setFanSpeed(unsigned int fanSpeed);
      unsigned int getFanSpeed();

 /** This function is not a member of class frienddemo **/
      friend void printValues(frienddemoFunction &d);


};


 /** This function is not a member of class frienddemo **/
  friend void printValues(frienddemoFunction &d);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
dexterous
  • 6,422
  • 12
  • 51
  • 99

1 Answers1

9

No, it doesn't matter.

C++ standard, section § 11.3 / 9 [friend.class]

The meaning of the friend declaration is the same whether the friend declaration appears in the private, protected or public (9.2) portion of the class member-specification.

Note:

A static function declared within the class is still a class member. A friend function is not.

quantdev
  • 23,517
  • 5
  • 55
  • 88
  • Why static matters then? – dexterous Jan 04 '15 at 03:53
  • 3
    @dexterous_stranger : A friend function is not a member function. `static` is a modifier for a class member. A friendship declaration dos not declare a member, it merely grants special access rights to a non-member (of the current class) function. – quantdev Jan 04 '15 at 03:54