Chapter 33. Name Lookup and the Interface Principle—Part 3
Difficulty: 5
Take a few minutes to consider some implications of the Interface Principle on the way we reason about program design. We'll revisit a classic design problem: “What's the best way to write operator<<()
?”
There are two main ways to write operator<<()
for a class: as a free function that uses only the usual interface of the class (and possibly accessing nonpublic parts directly as a friend
), or as a free function that calls a virtual Print()
helper function in the class.
Which method is better? What are the tradeoffs?
Solution
Are you wondering why a question like this gets a title ...
Get Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions 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.