Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

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;
};
 
PREVIOUS NEXT
Tagged: #prevent #copy
ADD COMMENT
Topic
Name
3+6 =