O'Reilly logo

Efficient C++ Performance Programming Techniques by David Mayhew, Dov Bulka

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

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 ...

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