Chapter 2. The Semantics of Constructors

One of the most often heard complaints about C++ is that the compiler does things behind the programmer’s back. Conversion operators are the example most often cited. There’s a story that Jerry Schwarz, the architect of the iostream library, tells about his first attempt to support a scalar test of an iostream class object such as

if ( cin ) ... 

For cin to evaluate to a true/false scalar value, Jerry first defined an operator int() conversion operator. This worked fine in well-behaved instances such as this example, but it behaved in a somewhat surprising manner under the following programmer error:

// oops: meant cout, not cin 
cin << intVal; 

The programmer, of course, meant cout not cin. The type-safe ...

Get Inside the C++ Object Model 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.