Friend Member Functions
Looking at the code for the last example, you may notice that most of the Remote
methods are implemented by using the public interface for the Tv
class. This means that those methods don’t really need friend status. Indeed, the only Remote
method that accesses a private Tv
member directly is Remote::set_chan()
, so that’s the only method that needs to be a friend. You do have the option of making just selected class members friends to another class rather than making the entire class a friend, but that’s a bit more awkward. You need to be careful about the order in which you arrange the various declarations and definitions. Let’s look at why.
The way to make Remote::set_chan()
a friend to the Tv
class is to declare it as ...
Get C++ Primer Plus 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.