Arrays 固定对象的赋值';通过智能指针创建指针成员

Arrays 固定对象的赋值';通过智能指针创建指针成员,arrays,c++11,unique-ptr,assignment-operator,move-assignment-operator,Arrays,C++11,Unique Ptr,Assignment Operator,Move Assignment Operator,我正在学习更多关于C++14中的智能指针的知识 考虑以下MWC: #include <iostream> #include <string> #include <memory> class House { public: House &operator=(const House &house) = default; House(const House &house) = default; House(): id_habi

我正在学习更多关于C++14中的智能指针的知识

考虑以下MWC:

#include <iostream>
#include <string>
#include <memory>

class House {
 public:
  House &operator=(const House &house) = default;
  House(const House &house) = default;
  House(): id_habitants_(nullptr), num_habitants_() {}
  explicit House(size_t num_habitants) {
    if (num_habitants > 0) {
      num_habitants_ = num_habitants;
      id_habitants_ = new int[num_habitants_];
      if (id_habitants_ != nullptr) {
        for (size_t id = 0; id < num_habitants_; ++id) {
          id_habitants_[id] = 1;
        }
      }
    }
  }
  void Print() {
    if (id_habitants_ != nullptr) {
      for (size_t id = 0; id < num_habitants_; ++id) {
        std::cout << id_habitants_[id] << ' ';
      }
      std::cout << std::endl;
    } else {
      std::cout << "<empty>" << std::endl;
    }
  }
  ~House() {
    if (id_habitants_ != nullptr) {
      delete [] id_habitants_;
    }
    num_habitants_ = 0;
  }
 private:
  int *id_habitants_;
  size_t num_habitants_;
};

int main() {
  std::cout << "Testing unique_ptr.\n" << std::endl;

  std::cout << "Using a dumb House class..." << std::endl;
  std::cout << "Creating House h1 with 3 habitants..." << std::endl;
  House h1(3);
  std::cout << "IDs of h1's 3 habitants:" << std::endl;
  h1.Print();
  std::cout << "Creating House h2 with 0 habitants..." << std::endl;
  House h2;
  std::cout << "IDs of h2's 0 habitants:" << std::endl;
  h2.Print();
  std::cout << "Default-assigning h1 to h2..." << std::endl;
  h2 = h1;
  std::cout << "IDs of h2's new 3 habitants:" << std::endl;
  h2.Print();
  std::cout << "Destroying h1..." << std::endl;
  h1.~House();
  std::cout << "IDs of h2's new 3 habitants:" << std::endl;
  h2.Print();
}
根据,我无法将一个唯一指针复制到另一个。有道理,对吧?这有点违背了它独特的目的。也就是说,这不会编译:

SmartHouse sh1(3);
SmartHouse sh2;
sh2 = sh1;
但我可以指定一个移动分配操作符,并在分配时移动
unique_ptr
成员,从而在分配时将指向数据的所有权转移到左侧对象:

class SmartHouse {
  SmartHouse &operator=(SmartHouse &&SmartHouse) = default;
}
...
SmartHouse sh1(3);
SmartHouse sh2;
sh2 = std::move(sh1);
sh1.~SmartHouse();
sh2.Print();
核心问题:这有意义吗?有没有更好的方法来增强指针成员变量的赋值


.

您似乎想要一份深入的
id\u居住者
。执行此操作的“智能指针”称为
std::vector
。作为奖励,它还使
num\u habitats\uuu
变得不必要,因为它跟踪自己的大小。1)
SmartHouse(size\u t num\u habitats)
对于0个居住者的情况没有任何作用。2)<代码> IDyHistalsSt=:STD::UnQuyJPPTR(P)< /> >可以拼写为“代码> IDLIVITANDSANS.RESET(P)< /COD> > 3),您需要认识到,与C或90年代早期的原始C++不同,在现代C++分配函数中(如<代码> new int [NUMIGIAATANDSI])/代码>在失败时抛出异常,它们不返回空指针,除了特殊的无例外版本(
new(std::nothrow)一些类型的
)。请参见4)您不能只执行
h1.~House()因为您将退出没有活动
h1
对象的块。在退出块之前,需要重新创建对象。(你只是不调用自动对象的析构函数。)5)你想用那个析构函数实现什么<代码>~SmartHouse(){num_habitats_u=0;}
当一个对象被销毁时,它的子对象也会被销毁。
class SmartHouse {
  SmartHouse &operator=(SmartHouse &&SmartHouse) = default;
}
...
SmartHouse sh1(3);
SmartHouse sh2;
sh2 = std::move(sh1);
sh1.~SmartHouse();
sh2.Print();