Friends versus member functions

We do have to come back to the question, why not just make increase() a member function of the C class? In the example given in the preceding section, there's no reason to, really—increase() is clearly meant to be a part of the public interface of the C class, since it's one of the operations C supports. It needs special access to do its work, so it should be a member function. There are, however, cases where member functions come with limitations, or even cannot be used at all.

Let's consider an addition operator for the same C class—it's what is needed to make an expression like c1 + c2 compile if both variables are of type C. The addition, or operator+(), can be declared as a member function:

class C { ...

Get Hands-On Design Patterns with C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.