The Mechanics of Return-by-Value
The Complex class implements a representation for complex numbers:
class Complex { // Complex addition operator friend Complex operator+(const Complex&, const Complex&); public: // Default constructor. // Value defaults to 0 unless otherwise specified. Complex (double r = 0.0, double i = 0.0) : real (r), imag (i) {} // Copy constructor Complex (const Complex& c) : real (c.real), imag (c.imag) {} // Assignment operator Complex& operator= (const Complex& c); ~Complex() {} private: double real; double imag; };
The addition operator returns a Complex object by value, as in:
Complex operator+ (const Complex& a, const Complex& b) { Complex retVal; retVal.real = a.real + b.real; retVal.imag = a.imag + b.imag; return ...
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.