简体   繁体   English

参数化构造函数的调用是如何执行的?

[英]How the invoking of parameterized constructor is executed?

There is this piece of code which have friend function and operator overloading , i'm getting an output that is making partially sense to be , so here is the code , the thing i'm not getting is that how the constructor having float type parameter is being called when the calls made in the are with object parameters.有这段代码具有友元函数和运算符重载,我得到的输出部分有意义,所以这是代码,我没有得到的是构造函数如何具有浮点类型参数当使用对象参数在 中进行的调用时被调用。

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;
}

and there goes the main part然后是主要部分

int main()
{
    a f1;

    float f=1.5;

    f1+f;

}

the problem exactly is how the parameterised constructor or the type casting contructor is getting invoked?问题究竟是如何调用参数化构造函数或类型转换构造函数?

Output:
default constructor called


type casting constructor called
copy constructor called
copy constructor called

...

If I've got this right, you're wondering why the a(float f) constructor gets called when you add a float to an existing instance of a .如果我有这个权利,你想知道为什么a(float f)当你添加一个构造函数被调用float到现有实例a

So, this is caused by implicit construction, and it's happening because of a few things:所以,这是由隐式构造引起的,它的发生是因为以下几点:

  1. You have a constructor which takes a float as a parameter.你有一个构造函数,它接受一个float作为参数。
  2. You have an operator+ overload which takes two instances of a .你有一个operator+超载这需要的两个实例a

So when you perform your addition, the right-hand side variable is a float .因此,当您执行加法时,右侧的变量是一个float Since you can instantiate a with a float, the constructor gets called to create an instance of a to add it to your left-hand side instance.既然你可以实例化a有浮动,构造函数被调用创建的实例a将其添加到您的左侧实例。 You then get two copies, because the operator+ function takes two instances of a by copy.然后你会得到两个副本,因为operator+函数接受a by copy 的两个实例。

A step-by-step breakdown would be this:分步分解是这样的:

  • 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+(ax, ay); // Takes two copies, so outputs "copy constructor called" twice

Since I explained what's going on, I figure I should also explain how to avoid it.既然我解释了发生了什么,我想我也应该解释如何避免它。

If, for whatever reason, you don't want the implicit construction to take place, you can do one of two things:如果出于某种原因,您希望进行隐式构造,则可以执行以下两种操作之一:

  • Prefix the constructor that takes a float parameter with the explicit keyword, which means that constructor will never be called as part of an implicit conversion or copy-initialization.使用explicit关键字为采用float参数的构造函数添加前缀,这意味着构造函数永远不会作为隐式转换或复制初始化的一部分被调用。 It will also no longer allow you to pass any parameters that could be converted into a float to it implicitly.它也将不再允许您将任何可以转换为float参数隐式传递给它。
  • Declare and define another operator+ function that takes a float as its right-hand side parameter.声明并定义另一个operator+函数,该函数将float作为其右侧参数。 Something like friend a operator+(ax, float y) .friend a operator+(ax, float y) This operator will be called instead of the current one, since it doesn't require a conversion to work.将调用此运算符而不是当前的运算符,因为它不需要转换即可工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM