Type Mismatch

The previous example is a special case of the more general type mismatch. We tried to initialize an object of type Rational with an integer. The generic case of type mismatch is any time an object of type X is expected and some other type is provided. The compiler needs, somehow, to convert the provided type into the expected object of type X. A temporary may get generated in the process. Look at the following:

{
    Rational r;
    r = 100;
    ...
}

Our Rational class did not declare an assignment operator that takes an integer parameter. The compiler, then, expects a Rational object on the right-hand side that will be bit-blasted to the left-hand side. The compiler must find a way to convert the integer argument we provided into an object ...

Get Efficient C++ Performance Programming Techniques 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.