Saturday, August 7, 2010

What is a friend class?what are theaccess control a friend class has over the member function?

C++What is a friend class?what are theaccess control a friend class has over the member function?
A friend class is a class that has same access permissions/control for your class's members (functions and variables).


Usually a class's private and protected members are not accessible to the code outside the class (e.g. free functions, other classes).


In special cases you might want to allow access to your protected/private members to some external class/function. This is where you declare that external class/function to be a friend of your class. This declaration of friendship is inside your own class.


One such usual case is when you're testing (unit/module/white-box testing) your class the tester class/function might need to access the protected/private members. In such a case you declare the tester to be a friend of your class.


In general it is advisable NOT to declare friendship to a class. Instead declare only the required/bare-minimum functions as friends.
  • sunscreen
  • No comments:

    Post a Comment