📜  disallowcopy c++ 代码示例

📅  最后修改于: 2022-03-11 14:44:46.155000             🧑  作者: Mango

代码示例1
Method 1: Private copy constructor and copy assignment operator
  class Car {
public:
  Car(): owner() {}
  void setOwner(Person *o) { owner = o; }
  Person *getOwner() const { return owner; }
  void info() const;
private:
  Car(const Car&);
  Car& operator=(const Car&);
  Person *owner;
};

Method 2: Deleted copy constructor and copy assignment operator
  class Car {
public:
  Car(const Car&) = delete;
  void operator=(const Car&) = delete;
  Car(): owner() {}
  void setOwner(Person *o) { owner = o; }
  Person *getOwner() const { return owner; }
private:
  Person *owner;
};