Reputation: 11753
I have a question with friend function in C++. I understand that if a function is defined as a friend function of a class, it can access any member variables or function regardless whether it is private, protected or public. Recently I am using doxygen to create document reference, I find another advantage of friend function: its relation with the class can be easily illustrated as friend function will be listed after the member function in the HTML page. However, if the function is not defined as friend, it will be regarded as a global function, and with doxygen it will not be listed with the class documentation. Then I plan to make all the global functions that have relationship with a specific class as its friend functions. I was wondering whether this is a good practice.
The following codes show one function can be chosen as either as a friend function or a global function.
#include <iostream>
#include <map>
#include <set>
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <numeric>
#include <string>
using namespace std;
class ABCD
{
public:
int a;
int b;
friend void friend_fun(const ABCD &obj);
};
void fun(const ABCD &obj)
{
std::cout<<obj.a<<endl;
std::cout<<obj.b<<endl;
};
void friend_fun(const ABCD &obj)
{
std::cout<<obj.a<<endl;
std::cout<<obj.b<<endl;
};
int main ()
{
ABCD obj;
obj.a = 20;
obj.b = 30;
fun(obj);
friend_fun(obj);
return 0;
}
Upvotes: 2
Views: 2740
Reputation: 11753
Basic on the answers, it is definitely not a good practice to declare all global functions as friend functions. Doxygen also provides a keyword to connect the functions that has close relations with the class: /relates
Upvotes: 0
Reputation: 254561
Doxygen allows you to create and refer to groups of functions and other global things. That would make a lot more sense than breaking useful language-level protection just to exploit a quirk of Doxygen's behaviour.
Upvotes: 6
Reputation: 143
I do agree with Mike's view here, though wanted to mention few more points regarding the friend functions that I understood. - The friend functions are not transitive. This keeps the friend functions local to the class in which they are declared to be local. - The friend functions do not inherit. This way the friend functions are friends only to that class which declares them to be friend. It is completely conscious decision of the class owner to declare the specific function to be friend and not all. - While in case of the member functions [getter and setter], they become available in the class inheritance chain. - Also functionality which needs the use of 2 independent classes can be achieved using the friend functions without making any changes to those classes.
Upvotes: 0
Reputation: 1775
Using friend function mechanism is never a good practice, so try to avoid it. Nothing from outside of the definition of a class should've an access to its private data. C++ encourage you to use encapsulation/modular mechanism and friend definition only weakens this mechanism.
Upvotes: 1
Reputation: 8027
I would not change my code just to fit in with the foibles of a documentation tool. So, no, I wouldn't consider it good practise.
Upvotes: 2