为什么返回运算符重载允许的构造函数?为什么在运算符重载中返回允许的构造函数?
下面是一个例子:
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);
}
这似乎为对象,而不是对象本身将返回一个构造函数?那里有什么回报?
这似乎更有意义:
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;
}
'复(...)'是*实例*'的Complex'建设。构造函数用于初始化该类型的对象,在这种情况下为临时对象。 – 0x499602D2 2014-09-01 23:45:34
“返回构造函数”?你在地球上得出的结论是它“返回一个构造函数”?在C++语言中,构造函数的名称如“Complex :: Complex”。注 - 两个相同的部分用'::'分隔。这将是一个构造函数名称。现在,您在哪里看到任何在您发布的代码中看起来像构造函数的东西? – AnT 2014-09-02 00:22:57
为什么不'返回{真实,虚构};'? – 2014-09-02 08:38:36