参数化构造函数的调用是如何执行的?
How the invoking of parameterized constructor is executed?
这段代码有友元函数和运算符重载,我得到的输出部分有意义,所以这是代码,我没有得到的是当在其中进行的调用带有对象参数时,将调用具有浮点类型参数的构造函数。
class a
{
public:
a():n(3),d(2)
{
cout<<endl<<"default constructor called";
}
a(float f):n(f),d(1)
{
cout<<endl<<"type casting constructor called";
}
a(const a &f)
{
cout<<endl<<"copy constructor called";
}
friend a operator+( a x, a y);
};
a operator+(a x, a y)
{
return x;
}
然后是主要部分
int main()
{
a f1;
float f=1.5;
f1+f;
}
问题到底是如何调用参数化构造函数或类型转换构造函数?
Output:
default constructor called
type casting constructor called
copy constructor called
copy constructor called
...
如果我没记错的话,您想知道为什么在将 float
添加到 a
.[=31 的现有实例时调用 a(float f)
构造函数=]
所以,这是由隐式构造引起的,它的发生是因为以下几点:
- 你有一个以
float
作为参数的构造函数。
- 您有一个
operator+
重载,它需要 a
. 的两个实例
所以当你执行加法时,右边的变量是 float
。由于您可以使用浮点数实例化 a
,因此会调用构造函数来创建 a
的实例,以将其添加到您的左侧实例。然后你得到两个副本,因为 operator+
函数通过副本获取 a
的两个实例。
一步一步的细分是这样的:
a f1; // Outputs "default constructor called"
f1 + f; // "f" is a float, so we can construct an instance of "a".
f1 + a(f); // Outputs "type casting constructor called"
operator+(a x, a y); // Takes two copies, so outputs "copy constructor called" twice
既然我解释了发生了什么,我想我也应该解释一下如何避免它。
如果出于某种原因,您不想进行隐式构造,您可以执行以下两项操作之一:
- 使用
explicit
关键字为采用 float
参数的构造函数添加前缀,这意味着构造函数永远不会作为隐式转换或复制初始化的一部分被调用。它还将不再允许您将任何可以转换为 float
的参数隐式传递给它。
- 声明并定义另一个
operator+
函数,该函数将 float
作为其右侧参数。像 friend a operator+(a x, float y)
这样的东西。将调用此运算符而不是当前运算符,因为它不需要转换即可工作。
这段代码有友元函数和运算符重载,我得到的输出部分有意义,所以这是代码,我没有得到的是当在其中进行的调用带有对象参数时,将调用具有浮点类型参数的构造函数。
class a
{
public:
a():n(3),d(2)
{
cout<<endl<<"default constructor called";
}
a(float f):n(f),d(1)
{
cout<<endl<<"type casting constructor called";
}
a(const a &f)
{
cout<<endl<<"copy constructor called";
}
friend a operator+( a x, a y);
};
a operator+(a x, a y)
{
return x;
}
然后是主要部分
int main()
{
a f1;
float f=1.5;
f1+f;
}
问题到底是如何调用参数化构造函数或类型转换构造函数?
Output:
default constructor called
type casting constructor called
copy constructor called
copy constructor called
...
如果我没记错的话,您想知道为什么在将 float
添加到 a
.[=31 的现有实例时调用 a(float f)
构造函数=]
所以,这是由隐式构造引起的,它的发生是因为以下几点:
- 你有一个以
float
作为参数的构造函数。 - 您有一个
operator+
重载,它需要a
. 的两个实例
所以当你执行加法时,右边的变量是 float
。由于您可以使用浮点数实例化 a
,因此会调用构造函数来创建 a
的实例,以将其添加到您的左侧实例。然后你得到两个副本,因为 operator+
函数通过副本获取 a
的两个实例。
一步一步的细分是这样的:
a f1; // Outputs "default constructor called"
f1 + f; // "f" is a float, so we can construct an instance of "a".
f1 + a(f); // Outputs "type casting constructor called"
operator+(a x, a y); // Takes two copies, so outputs "copy constructor called" twice
既然我解释了发生了什么,我想我也应该解释一下如何避免它。
如果出于某种原因,您不想进行隐式构造,您可以执行以下两项操作之一:
- 使用
explicit
关键字为采用float
参数的构造函数添加前缀,这意味着构造函数永远不会作为隐式转换或复制初始化的一部分被调用。它还将不再允许您将任何可以转换为float
的参数隐式传递给它。 - 声明并定义另一个
operator+
函数,该函数将float
作为其右侧参数。像friend a operator+(a x, float y)
这样的东西。将调用此运算符而不是当前运算符,因为它不需要转换即可工作。