5

Why is returning a constructor allowed in operator overloading?

Here is an example:

Complex Complex::operator*( const Complex &operand2 ) const
{
    double Real = (real * operand2.real)-(imaginary * operand2.imaginary);
    double Imaginary = ( real * operand2.imaginary)+(imaginary * operand2.real);

    return Complex ( Real, Imaginary );
}

It seems to be returning a constructor for the object and not the object itself? What is it returning there?

This seems to make more sense:

Complex Complex::operator*( const Complex &operand2 ) const
{
    double Real = (real * operand2.real)-(imaginary * operand2.imaginary);
    double Imaginary = ( real * operand2.imaginary)+(imaginary * operand2.real);

    Complex somenumber ( Real, Imaginary );

    return somenumber;
}
Overtaker
  • 71
  • 5
  • 7
    `Complex(...)` is the construction of an *instance* of `Complex`. The constructor is used to initialize an object of that type, in this case, a temporary. – David G Sep 01 '14 at 23:45
  • 3
    "returning a constructor"??? What on Earth made you conclude that it is "returning a constructor"? In C++ language constructors have names like `Complex::Complex`. Note - two identical parts separated by a `::`. That would be a constructor name. Now, where do you see anything that would look like a constructor in the code you posted? – AnT stands with Russia Sep 02 '14 at 00:22
  • Why not `return {Real, Imaginary};`? – Kerrek SB Sep 02 '14 at 08:38
  • I have never seen that syntax before. Does the syntax work same as how Matt answered below? – Overtaker Sep 02 '14 at 15:04

1 Answers1

10

In C++ the syntax: Typename ( arguments ) means to create an unnamed object (aka. a temporary object) of type Typename 1. The arguments are passed to the constructor of that object (or used as initializers for primitive types).

It is very similar in meaning to your second example:

Complex somenumber( Real, Imaginary);
return somenumber;

the only difference being that the object has a name.

There are some minor differences between the two versions relating to copying or moving the object back to the calling function. More info here

1 Except when it's part of a function declaration

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
  • Matt, I edited the second example. Yeah it didn't make much sense since the () operator was not overloaded in Complex. – Overtaker Sep 01 '14 at 23:55
  • As for the copy/move constructor thing you mentioned, is it better to use the first version and return an unnamed temporary object? – Overtaker Sep 02 '14 at 00:17
  • 1
    @Overtaker I think they are technically equivalent , although historically some compilers elided case 1 but not case 2. (I'm not completely sure about this though) – M.M Sep 02 '14 at 00:18
  • Okay, thanks for the help! I see books using case 1, so I may just stick to that. As for Typename (arguments), did you mean to say Class name (arguments)? – Overtaker Sep 02 '14 at 00:44
  • 1
    @Overtaker No. A class name is a type name but you can also do it with non-classes (e.g. `return int(5);` – M.M Sep 02 '14 at 02:04