首页 > 解决方案 > 参数化构造函数的调用是如何执行的?

问题描述

这段代码有友元函数和运算符重载,我得到的输出部分有意义,所以这里是代码,我没有得到的是构造函数如何具有浮点类型参数当在 中进行的调用带有对象参数时被调用。

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

...

标签: c++operator-overloadingcopy-constructordefault-constructorfriend-function

解决方案


如果我做对了,您想知道为什么a(float f)当您将 a 添加floata.

所以,这是由隐式构造引起的,它的发生是因为有几件事:

  1. 您有一个以 afloat作为参数的构造函数。
  2. 您有一个operator+重载,它需要两个a.

因此,当您执行加法时,右侧变量是 a float。由于您可以a使用浮点数进行实例化,因此会调用构造函数来创建一个实例a以将其添加到您的左侧实例中。然后你会得到两个副本,因为该operator+函数需要两个aby copy 实例。

一步一步的细分是这样的:

  • 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

既然我解释了发生了什么,我想我也应该解释如何避免它。

如果出于某种原因,您希望发生隐式构造,则可以执行以下两项操作之一:

  • float使用关键字为带有参数的构造函数添加前缀explicit,这意味着构造函数永远不会作为隐式转换或复制初始化的一部分被调用。它也将不再允许您将任何可以转换为 a 的参数float隐式传递给它。
  • 声明并定义另一个operator+以 afloat作为其右侧参数的函数。类似的东西friend a operator+(a x, float y)。将调用此运算符而不是当前运算符,因为它不需要转换即可工作。

推荐阅读