复制构造函数的 2 种不同类型的构造函数调用

发布于 2024-12-24 16:48:24 字数 1047 浏览 1 评论 0原文

考虑下面的示例代码:

#include <iostream>

using namespace std;

class core
{
   public:
      core(const core& obj)
      {
         cout << "core copy ctor called\n";
      }
      core()
      {
         cout << "core default ctor called\n";
      }
};

class sample : public core
{
   public:
      sample()
      {
         cout << "sample default ctor called\n";
      }
#if 0
      sample(const sample& obj)
      {
         cout << "sample copy ctor called\n";
      }
#endif
};

int main()
{
   sample s1;
   sample s2 = s1; //Line1
   return 0;
}

Type1:未为类sample显式声明复制构造函数

(Type1如上面的代码所示。然后类sample的复制构造函数由编译器隐式生成)。 当执行Line1语句时,首先调用class core的复制构造函数,然后调用class example的复制构造函数。

类型2:复制为类示例显式定义的构造函数

当执行Line1语句时,首先调用class core的默认构造函数,然后调用class example的复制构造函数。

问题

为什么 Type1 和 Type2 中提到的复制构造函数的行为存在差异?

Consider the sample code below:

#include <iostream>

using namespace std;

class core
{
   public:
      core(const core& obj)
      {
         cout << "core copy ctor called\n";
      }
      core()
      {
         cout << "core default ctor called\n";
      }
};

class sample : public core
{
   public:
      sample()
      {
         cout << "sample default ctor called\n";
      }
#if 0
      sample(const sample& obj)
      {
         cout << "sample copy ctor called\n";
      }
#endif
};

int main()
{
   sample s1;
   sample s2 = s1; //Line1
   return 0;
}

Type1: Copy constructor not declared explicitly for class sample

(Type1 is shown in the code above. Then the copy constructor of class sample is implicitly generated by the compiler).
When the statement, Line1 is executed, first the copy constructor of class core is invoked, and then the copy constructor of class sample is invoked.

Type2: Copy constructor defined explicitly for class sample

When the statement, Line1 is executed, first the default constructor of class core is invoked, and then the copy constructor of class sample is invoked.

Question:

Why is this difference in behavior for copy constructor as mentioned in Type1 and Type2 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿哥们儿 2024-12-31 16:48:24

因为您为 sample 显式定义的复制构造函数不会要求调用 core 的复制构造函数。如果你想实现这一点,你必须编写 : core(obj)

换句话说,当您编写显式复制构造函数时,您将负责 sample 的复制构造,包括其 core 子对象。通过按照您所做的方式编写它,您选择不使用 obj 初始化 core 子对象。由于您没有说明您希望如何初始化它,编译器仅使用 core 的默认构造函数,因此您概述了第二种情况的行为。

相比之下,在第一种情况下,编译器为 sample 生成的默认复制构造函数确实要求使用 core 初始化 core 子对象> 的复制构造函数,因此观察到的行为。

Because the copy constructor you explicitly define for sample doesn't ask for core's copy constructor to be invoked. You'd have to write : core(obj) if you wanted to make that happen.

Put another way, when you write an explicit copy constructor, you are taking charge of the copy construction of sample, including its core sub-object. By writing it the way you have done, you have chosen not to initialise the core sub-object using obj. Since you haven't said how you do want it initialised, the compiler just uses core's default constructor instead, hence the behaviour in the second case you outline.

By contrast, in the first case, the compiler-generated default copy constructor for sample does ask for the core sub-object to be initialised using core's copy constructor, hence the observed behaviour.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文