Tutorial :friend class/function in c++ [duplicate]



Question:

Possible Duplicate:
When should you use 'friend' in C++?

I see a lot of people recommending a function/class to be made a friend of another class here in SO though there are other alternatives. Shouldn't friend be sparingly used in C++? I feel other options must be considered before deciding on using the friend feature. Opinions/suggestions are welcome.


Solution:1

Without specific examples this is hard to decide. While friend isn't strictly necessary it does have its uses. If, as you claim, there are better alternatives then obviously use them, by simple definition of the word “better”. Or maybe the decision which solution is better isn't that clean-cut after all.

Personally, I prefer to avoid it when possible but I prefer to use it over method duplication: for example, I do not like to write a print method just to avoid making operator << a friend because I don't see the benefit of the duplicate method.


Solution:2

Some say friend is a violation of encapsulation. In my opinion, it is the exact contrary. Without friend, we would end up with either:

  • huge monolithic classes, that does plenty of things they were not meant to, just to avoid letting outer classes access their internals
  • badly encapsulated classes: to separate concerns and write classes with one and only one responsability, we would need to provide public access to internal data needed by the related set of classes.

friend is a good way to overcome this issue, since it lets you free to separate responsabilities in several classes, while at the same time letting you restrict access to implementation details to the few functions or types which need them.


Solution:3

I agree. The friend keyword should be used sparingly.

It may be useful if you have a set of classes that interact together when you want to expose a clean api from those classes to users, but when the classes can interact with each other using a richer interface.

Eg:

class Folder  {  public:    int GetFileCount();  private:    void IncrementFileCount();  // users of this class have no right to use this      friend class File;  };    class File  {  public:    File(string name, Folder& containingFolder)    {       containingFolder.IncrementFileCount(); // except for indirectly when they create a file    }  };  


Solution:4

For all those who thinks friend violates the encapsulation, here is what Bjarne Stroustup has to say .

But I personally don't use friend unless it is inevitable. Scenarios like implementation of Iterator patterns there is no other choice.

Friend is friend if used properly otherwise he is enemy!


Solution:5

Friend functions are advantageous in cases where you would want to call a 3rd party library function which needs access to members of your class, consider for example:

class A {  private:       int x,y;       double result;  public:       friend void *power(void *x);  }    
You can now call the pow function found in math.h using this friend function.Your power function can now be defined as:
  void *power(void *X)  {       A *a;       a = static_cast< A *> (X);       a->result = pow(a->x,a->y);       return NULL;  }  
Although there are easier ways of calling the pow function.This example is only meant to illustrate the importance of friend function in calling library functions. Hope it is useful.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »