dexterous
dexterous

Reputation: 6526

Does access specifier matters for a friend function?

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);

Upvotes: 7

Views: 1388

Answers (1)

quantdev
quantdev

Reputation: 23813

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.

Upvotes: 9

Related Questions