Rule of 5 模板

class 类名 {
public:
类名()
{
   // 在此实现默认构造函数(初始化资源,例如 new XXX)
}
~类名()
{
   // 在此实现析构函数(释放资源,例如 delete XXX)
}
类名(类名 const &other)
{
   // 在此实现拷贝构造函数,确保实现深拷贝
}
void swap(类名 &other) noexcept
{
    std::swap(资源名称, other.资源名称);
}
类名(类名 && other) noexcept
   :类名() 
{
   this->swap(other);
}
类名 &operator=(类名 const &other)
{
   类名 t(other);
   this->swap(t);
   return *this;
}
类名 &operator=(类名 &&other) noexcept
{
   类名 t(std::move(other));
   this->swap(t);
   return *this;
}
private:
   资源类型 资源名称;
};
posted @ 2022-07-14 16:41  BinarySong  阅读(37)  评论(0编辑  收藏  举报