1. Use "=default"
1. Explicitly generate synthetic versions of copy control members
class A { public: A() = default; A(const A &) = default; A& operator=(const A &) = default; ~A() = default; };
2. You can only use '=default' for member functions with synthetic versions
- default constructor
- Copy control members (copy constructor, copy assignment operator, destructor, move constructor, move assignment operator)
3. Synthetic functions generated using "=default" are divided into inline and non-inline
class A { public: A() = default; // use =default-->inline in class A(const A &) = default; // use =default-->inline in the class A& operator=(const A &); }; A& A::operator=(const A &) = default; // outside the class = default --> not inline
2. Use "=delete"
1. Define the related function as the deleted function
- Deleted function: We declare it, but cannot use it in any way.
2. =delete must appear when the function is first declared
3. You can specify =delete for any function
- The main purpose of the deleted function is to prohibit copying of control members
4. Destructor cannot use "=delete"
5. Example
class A { public: A() = default; // use synthetic default constructor A(const A &) = delete; // prevent copying A& operator=(const A &) = delete; // prevent assignment ~A() = default; // use synthetic destructor };