首页 > 解决方案 > 通过智能指针修复对象指针成员的分配

问题描述

我正在学习更多关于 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();
}

在不修改 class 的默认复制构造函数和默认赋值运算符的情况下House,如何通过智能指针在赋值期间确保正确的指针行为?

第一次尝试似乎使用std::unique_ptr将是要走的路。我可以创建一个新类:

class SmartHouse {
 public:
  SmartHouse &operator=(const SmartHouse &shouse) = default;
  SmartHouse(const SmartHouse &shouse) = default;
  SmartHouse(): id_habitants_(nullptr), num_habitants_() {}

  explicit SmartHouse(size_t num_habitants) {
    if (num_habitants > 0) {
      num_habitants_ = num_habitants;
      id_habitants_ = std::unique_ptr<int[]>(new int[num_habitants_]);
      if (id_habitants_) {
        for (size_t id = 0; id < num_habitants_; ++id) {
          id_habitants_[id] = 1;
        }
      }
    }
  }
  void Print() {
    if (id_habitants_) {
      for (size_t id = 0; id < num_habitants_; ++id) {
        std::cout << id_habitants_[id] << ' ';
      }
      std::cout << std::endl;
    } else {
      std::cout << "<empty>" << std::endl;
    }
  }
  ~SmartHouse() {
    num_habitants_ = 0;
  }
 private:
  std::unique_ptr<int[]> id_habitants_;
  size_t num_habitants_;
};

据此我不能真正将一个唯一指针复制到另一个。有道理,对吧?它有点违背了它独特的目的。即这不会编译:

SmartHouse sh1(3);
SmartHouse sh2;
sh2 = sh1;

但是我可以指定一个移动赋值运算符,并unique_ptr<int[]>在赋值时移动成员,从而在赋值时将指向数据的所有权转移到左侧对象:

class SmartHouse {
  SmartHouse &operator=(SmartHouse &&SmartHouse) = default;
}
...
SmartHouse sh1(3);
SmartHouse sh2;
sh2 = std::move(sh1);
sh1.~SmartHouse();
sh2.Print();

核心问题:这有意义吗?有没有更好的方法来增强指针成员变量的赋值?

完整的 MWE

标签: arraysc++11unique-ptrassignment-operatormove-assignment-operator

解决方案


推荐阅读