-4
// operator_overloading.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

struct Complex {
   Complex( double r, double i ) : re(r), im(i) {} // what is this syntax?
   Complex operator+( Complex &other );
   void Display( ) {   cout << re << ", " << im << endl; }
private:
   double re, im;
};

// Operator overloaded using a member function
Complex Complex::operator+( Complex &other ) {
   return Complex( re + other.re, im + other.im );
}

int main() {
   Complex a = Complex( 1.2, 3.4 );
   Complex b = Complex( 5.6, 7.8 );
   Complex c = Complex( 0.0, 0.0 );

   c = a + b;
   c.Display();
}
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
Naman
  • 2,363
  • 5
  • 26
  • 27
  • similar: http://stackoverflow.com/questions/4289899/advantages-of-using-initializer-list http://stackoverflow.com/questions/1842678/c-newbie-initializer-list-question – Ray Tayek Oct 10 '11 at 07:09
  • possible duplicate of [What is the member variables list after the colon in a constructor good for?](http://stackoverflow.com/questions/210616/what-is-the-member-variables-list-after-the-colon-in-a-constructor-good-for) – Björn Pollex Oct 10 '11 at 07:10
  • possible duplicate of [What is this weird colon-member syntax in the constructor?](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor) – Flexo Mar 27 '12 at 09:10

3 Answers3

5
Complex( double r, double i ) : re(r), im(i) {}

This constructor is called a Member Initializer List in C++.

It initializes your member re to a value r, and member im to a value i.


What is the difference between Initializing And Assignment inside a constructor? &
What is the advantage?

There is a difference between Initializing a member using initializer list and assigning a value to it inside the constructor body.

When you initialize fields via initializer list the constructors will be called once.

If you use the assignment then the fields will be firstly initialized with default constructor's data and then reassigned (via assignment operator) with actual values.

As you see there is an additional overhead of creation & assignment in the latter, which might be considerable for user defined classes.

For a double data type (for which you use it) or POD class members there is no practical overhead.

Louis Langholtz
  • 2,913
  • 3
  • 17
  • 40
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Great answer. I still don't understand why using the standard assignment operator would call constructors. Shouldn't assignment just be assignment? If they're just pointers to objects then you would copy the value. For primitives it should be the same since there is no object, right? – styfle Nov 28 '11 at 19:41
  • 1
    @styfle: When members are assigned inside body of the constructor,two things happen, 1. The member objects are default constructed by calling their constructors & 2. Further they are assigned through assignment. While in case of Member Initializer list, the member object is constructed by calling appropriate constructor with parameter.Hence the overhead.And you are correct this overhead is not there in case of primitive data types but just its a good practice to use the member initializer list always as that ensures you do the same when you are dealing with non primitive types. – Alok Save Nov 29 '11 at 04:06
1

This is a initializer-list. It initializes the members of Complex.

An initializer-list can be used to explicitly initialize the members of a class. If you do not initialize your members in this way, they will get default-initialized. Some types of member have to be initialized in that way, for instance references (because they cannot be default initialized), or classes that have no default-constructor.

This syntax can also be used to pass parameters to the constructors of a superclass.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
1

Those are member initializers. When the object is created with that constructor, the members are initialized according to those.

So re gets set to r. And im gets set to i.

Mysticial
  • 464,885
  • 45
  • 335
  • 332