【问题标题】:How should I write a copy constructor to initialise another object?我应该如何编写一个复制构造函数来初始化另一个对象?
【发布时间】:2021-03-12 13:52:18
【问题描述】:

比如说,作为一个简化的例子,我有一个类对象 House,它也有一个对象 Kitchen。

这是头文件:

class Kitchen {
   private:
       int width;
       int height;
       int length;
   public:
       Kitchen(int width, height, length); // default constructor
};

class House {
   private:
       int houseId;
       Kitchen newKitchen;
   public:
       House(Kitchen newKitchen, int houseId); // default constructor
       House& operator=(House const& other); // copy assignment
       House(House const& other); // copy constructor
       ~House(); // destructor
};

在复制分配功能中复制houseId 工作正常。但我收到一个错误,指的是House::House(House const& other) { *this = other; },如下所示:

error: constructor for 'House' must explicitly initialize the member 'newKitchen' which does not have a default constructor

我不确定,因为我认为我的默认构造函数声明涵盖了这一点?

【问题讨论】:

  • 如果您的类成员中的所有内容都可以简单地复制,那么最好的选择就是根本不编写复制构造函数。编译器默认生成一个。
  • ... 和 Kitchen(int width, height, length);House(Kitchen newKitchen, int houseId); 不是默认构造函数。
  • 旁注:让赋值运算符使用复制构造函数通常更容易,而不是相反。当你有成员或者基类需要初始化的时候,赋值就不行了。

标签: c++ oop destructor


【解决方案1】:

首先,Kitchen 中的“默认构造函数”不是默认构造函数,它是用户定义的构造函数。它应该初始化成员,并且我会在 rule of five 之后重新启用复制和移动行为。

class Kitchen {
   private:
       int width;
       int height;
       int length;
   public:
       // Use member initialization list
       Kitchen(int _width, int _height, int _length) : width(_width), height(_height), length(_length) {}

       // Rule of 5
       Kitchen(Kitchen const&) = default;
       Kitchen& operator=(Kitchen const&) = default;
       Kitchen(Kitchen&&) = default;
       Kitchen& operator=(Kitchen&&) = default;
};

然后你的House 可以以类似的方式使用这个用户定义的构造函数

House(Kitchen _newKitchen, int _houseId) : houseId(_houseId), newKitchen(_newKitchen) {}

请注意,您的 Kitchen 可能只是一个 POD 聚合类型以节省麻烦

class Kitchen
{
public:
    int width;
    int height;
    int length;
}

这将遵循“零规则”,并且默认是可构造的、可聚合的、可初始化的、可复制的和可移动的。您的 House 课程也会如此。

【讨论】:

  • 您好,非常感谢您的评论。我已尝试实施您的建议,但仍然遇到相同的错误。我在用户定义的构造函数中为 House 和 Kitchen 创建了一个初始化列表
  • @mlan 如果你已经实现了 Cory 的建议,你就不会得到同样的错误。 Demo
猜你喜欢
  • 2016-01-08
  • 1970-01-01
  • 1970-01-01
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多