|A friend function basically gives access to the internal data members of a class to an external function.
This can definitely be achieved using getters and setters.
But in reality friend functions are not used for this purpose.
For example, if you look at the
CStringT class, the overloaded
+ operator is declared as a friend function. This is so that the
+ operator can be used to concatenate two string in the following 2 ways
CString cs1 = L"Hello";
CString cs2 = L"World";
CString cs3 = L"";
cs3 = cs1 + L" World";
cs3 = L"Hello " + cs2;
+ operator was an ordinary member function of the
CStringT class, then only the first call would have been possible.
I love work. It gives me something to do between weekends.