Is there an access specifier for a friend function?

In a class, if a function is declared as in each type specifier - private, protected or public, then there is some difference. In my understanding, the function of a friend is not a member. So it doesn’t matter. But, if I see static - it is also not a member, but the access specifier is of great importance. So, I'm a little confused. How does all this code work? Is there any difference between 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); 
+5
source share
1 answer

No, that doesn't matter.

C ++ Standard, Section § 11.3 / 9 [friend.class]

The value of a friend’s declaration is the same if the friend’s declaration appears in the private, protected or public (9.2) part of the class member specification.

Note:

A static function declared inside a class is still a member of the class. Friend's function is not.

+5
source

Source: https://habr.com/ru/post/1210340/


All Articles