复制构造函数或=运算符? [英] Copy constructor or = operator?

查看:153
本文介绍了复制构造函数或=运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    class Foo
    {


    };

    Foo f;
    Foo g = f; // (*)

我的问题是,在标有?
是否为默认副本ctr或'='运算符?

My question is, what is being called in the line marked with (*) ? Is it the default copy ctr or '=' operator?

推荐答案


是,在标有(*)的行中调用什么?是否为默认复制ctr或'='运算符?

My question is, what is being called in the line marked with (*) ? Is it the default copy ctr or '=' operator?

将调用复制构造函数。

The copy constructor will be called.

即使使用 = 符号,也是初始化 ,其中左侧的对象通过在右侧提供表达式作为其构造函数的参数来构造。

Even though the = sign is being used, this is a case of initialization, where the object on the left side is constructed by supplying the expression on the right side as an argument to its constructor.

特别地,这种初始化形式被称为 copy-initialization 。注意,当初始化器表达式的类型与初始化类对象( Foo ,在这种情况下)的类型相同时,复制初始化基本上等同于 direct-initialization ,即:

In particular, this form of initialization is called copy-initialization. Notice, that when the type of the initializer expression is the same as the type of the initialized class object (Foo, in this case), copy-initialization is basically equivalent to direct-initialization, i.e.:

Foo g(f); // or even Foo g{f} in C++11

复制构造函数 Foo 被标记为 explicit (很难想象为什么会是这种情况)将在复制初始化的情况下失败。

The subtle only difference is that if the copy constructor of Foo is marked as explicit (hard to imagine why that would be the case though), overload resolution will fail in the case of copy-initialization.

这篇关于复制构造函数或=运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆