Tag Archives: friend class/function

friend keyword C++

A friend function of a class is defined outside that class’ scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

A friend can be a function, function template, or member function, or a class (friend class )or class template, in which case the entire class and all of its members are friends.

If the operator does not modify any of its operands,but needs only a representation of the object, it does not have to be a member function.This is the reason why binary operators are often implemented as friend functions such as + , *, -, etc.

Conversely, = operator is not defined by friend function.If the operation modifies the state of the class object, it operates on, it must be a member function, not a friend function .Thus all operator such as =, *=, +=  etc. are naturally defined as member functions and not friend functions.

We can’t use the “this” pointer in the friend function since it is not the member function of the class. In case of the friend function, the obj1 and obj2 should be from the different classes. If they are from the same class, the compilation error will occur.

Below example shows syntax for overloading + operator using friend function for a class fraction:
fraction operator +(const fraction& amount1, const fraction& amount2)
class fraction
fraction(); //constructor for initialize
friend fraction operator +(fraction, fraction); //fraction add

Application of friend function:

To prevent accidental creation of an object on stack, make all constructors private, and/or make the destructor private, and provide friend or static functions that perform the same functionality. For example, here’s one that does both:
class X {
static X *New() {return new X;}
static X *New(int i) {return new X(i);}
void Delete(X *x) {delete x;}
X(int i);

Application of friend class:

Make the destructor private/protected  and provide a Destroyer class that is responsible for destroying and cleaning up all the memory of the Singleton. It will need to be a friend of the Singleton class.

Avoid Singleton Object being Destroyed by other thread/class Singleton; // forward declaration
Singleton * s = NULL; // global object available to all threads

// Place this class in a location unavailable to all threads except Main thread
class ManageSingleton
static void DestroySingleton(Singleton * s)
delete s;

class Singleton
friend class ManageSingleton;
~Singleton() {}

void main()
s = new Singleton;
while (…)
// other threads access Singleton object until program is finished

// Program is now finished; destroy Singleton object

Is it better to add functions that return the internal state of an object for unit testing, as opposed to making the testing class a friend? – especially, when there is no use for the functions except for the case of unit testing.

Using friend classes for unit testing is a perfectly legitimate and allows you to maintain encapsulation. You should not modify your classes public interface simply to make the class more testable. Think about it this way. What if you purchased a third party FTP library and you are trying to use it and it’s public interface is cluttered with a bunch of methods you don’t even need to know about simply because of unit tests! Even modifying a protected interface to compensate for unit tests is BAD.

If I am inheriting from some class, I don’t want to have to worry about what methods are useful to me and which ones are only there because of unit tests!!! Using friends classes for unit tests helps you maintain a simple, easier to use class interface; it helps to preserve encapsulation and abstraction!!!

friend class MyClassTest;

and now you can test your class any way you want!

Now I do agree that you should not use a friend class unless it is necessary. If you can test what needs testing without making it a friend, then please do it . But if life gets difficult and using a friend class makes life easy again, use it!