有簡單的代碼,我在C++教程中找到。但我不明白這一行:構造函數在這一行中做了什麼?
c1 = Complex(10.0);
在評論中寫道,可以使用構造函數從一種類型轉換到另一種。有人可以解釋這一刻。感謝您的任何幫助。
#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;
}
可能將雙數值10.0d轉換爲複數,實數部分等於10.0d,虛數部分等於零。 – Rekin