prevent copy c 2b 2b

Solutions on MaxInterview for prevent copy c 2b 2b by the best coders in the world

showing results for - "prevent copy c 2b 2b"
Nayeli
25 Feb 2019
1Method 1: Private copy constructor and copy assignment operator
2  class Car {
3public:
4  Car(): owner() {}
5  void setOwner(Person *o) { owner = o; }
6  Person *getOwner() const { return owner; }
7  void info() const;
8private:
9  Car(const Car&);
10  Car& operator=(const Car&);
11  Person *owner;
12};
13
14Method 2: Deleted copy constructor and copy assignment operator
15  class Car {
16public:
17  Car(const Car&) = delete;
18  void operator=(const Car&) = delete;
19  Car(): owner() {}
20  void setOwner(Person *o) { owner = o; }
21  Person *getOwner() const { return owner; }
22private:
23  Person *owner;
24};