Object Definition
Say that class Rational is declared as follows:
class Rational { friend Rational operator+(const Rational&, const Rational&); public: Rational (int a = 0, int b = 1 ) : m(a), n(b) {} private: int m; // Numerator int n; // Denominator };
We can instantiate objects of type Rational in several equivalent ways:
Rational r1(100); // 1 Rational r2 = Rational(100); // 2 Rational r3 = 100; // 3
Only the first form of initialization is guaranteed, across compiler implementations, not to generate a temporary object. If you use forms 2 or 3, you may end up with a temporary, depending on the compiler implementation. Take form 3 for example:
Rational r3 = 100; // 3
This form may lead the compiler to use the Rational::Rational(int, ...
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.