Answers for "prevent copy c++"

C++
0

prevent copy c++

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;
};
Posted by: Guest on August-11-2021

Browse Popular Code Answers by Language