veinhorn
veinhorn

Reputation: 2403

What does constructor do in this line?

There is the simple code, that i found in c++ tutorial. But I can't understand this line:

c1 = Complex(10.0);

In comments was written that constructor can be used to convert from one type to another. Can somebody explain this moment. Thank you for any help.

#include <iostream>
using namespace std;

class Complex
{
  public:
    Complex() : dReal(0.0), dImag(0.0)
    { cout << "invoke default constructor" << endl;}
    /*explicit*/ Complex(double _dReal)
      : dReal(_dReal), dImag(0.0)
    { cout << "invoke real constructor " << dReal <<endl;}
    Complex(double _dReal, double _dImag)
      : dReal(_dReal), dImag(_dImag)
    {
        cout << "invoke complex constructor " << dReal
             << ", " << dImag << endl;
    }

    double dReal;
    double dImag;
};

int main(int argcs, char* pArgs[])
{
    Complex c1, c2(1.0), c3(1.0, 1.0);

    // constructor can be used to convert from one type
    // to another
    c1 = Complex(10.0);

    // the following conversions work even if explicit
    // is uncommented
    c1 = (Complex)20.0;
    c1 = static_cast<Complex>(30.0);

    // the following implicit conversions work if the
    // explicit is commented out
    c1 = 40.0;
    c1 = 50;

    system("PAUSE");
    return 0;
}

Upvotes: 1

Views: 162

Answers (6)

Grzegorz
Grzegorz

Reputation: 3355

c1 = Complex(10.0); calls your implicit constructor /*explicit*/ Complex(double _dReal)

The comment says that you can convert double to Complex type.

In your case with explicit being removed, you are allowed to declare Complex c1 = 10.0 ; which will implicitly call Complex(double _dReal) for you. If explicit was not commented, however, it would forbid 'automatic' conversion, and you would be forced to declare Complex( 10.0 ) in order to 'convert' from double to Complex.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727047

This is not a conversion, because construct a Complex object explicitly:

c1 = Complex(10.0);

This, however, is a conversion:

c1 = (Complex)20.0;

and so is this:

c1 = static_cast<Complex>(30.0);

Both conversions above would work even if the constructor is explicit, because they invoke conversion, ...well, explicitly.

The other two would not work with an explicit constructor:

c1 = 40.0;
c1 = 50;

Here the conversion is implicit, i.e. the operation is implied by the type of the left-hand side of the assignment, and the presence of a constructor taking an expression on the right-hand side. The compiler cannot apply the constructor declared explicit in situations like this, so if you uncomment explicit in the declaration above, this snippet is not going to compile.

Upvotes: 3

The expression T(x) for any given type T and object x (or objects x1,x2...) creates a temporary of type T constructed with the given arguments.

Upvotes: 1

Nemanja Boric
Nemanja Boric

Reputation: 22187

This right here:

Complex(double _dReal)
      : dReal(_dReal), dImag(0.0)
    { cout << "invoke real constructor " << dReal <<endl;}

is a constructor which will take double and it will create Complex object from it. You can see that it will set the real part (dReal) to the value passed to the constructor (_dReal), and it will setup the imaginary part to the 0.

This line:

c1 = Complex(10.0);

will call that constructor, and it will convert passed real number (10.0) into the Complex object.

Edit: Please note that this is not the real conversion - it is explicitly creation of the Complex object by passing the double to it - you have examples of the conversions in the answer provided by dasblinkedlight.

Upvotes: 3

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14530

This

c1 = Complex(10.0);

calls in fact this:

Complex(double _dReal)
  : dReal(_dReal), dImag(0.0)
{
    cout << "invoke real constructor " << dReal <<endl;
}

This constructor just initializes the member dReal with the value passed in param and initializes dImag to 0.0.

When they say that you can convert one type to another, I guess they want to say that you can "convert" double to Complex.

Upvotes: 2

ChronoTrigger
ChronoTrigger

Reputation: 8617

c1 = Complex(10.0); creates a Complex object and copies it to c1.

Upvotes: 1

Related Questions