O'Reilly logo

Exceptional C++: 47 Engineering Puzzles, Programming Problems, and Solutions by Herb Sutter

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

Solution

Are you wondering why a question like this gets a title ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required