【c++ 回顾】设计一个不可以复制的类

有了NotCopyable之后,怎样才能让Student不支持复制呢?大家还记得编译器生成默认的复制构造函数和赋值操作符重载的规则吗?如果父类不存在这些函数的话,那么子类默认也不会生成。那么最简单的做法就是让Student去继承自NotCopyable

代码

#include <cstdlib>
#include <string>

using namespace std;

class NotCopyable {
    NotCopyable() = default;
    NotCopyable(const NotCopyable &) = delete;
    NotCopyable(NotCopyable &&) = delete;
    NotCopyable& operator=(const NotCopyable &) = delete;
    NotCopyable& operator=(NotCopyable &&) = delete;
};

struct Student : private NotCopyable
{
    string name;
    time_t birtyday;
};

int main()
{
    Student a;
    Student b = a;
    a = b;
    return 0;
}
全部评论

相关推荐

优秀的大熊猫在okr...:多益:此贼,必有同谋,按律,该当连坐!
你不能接受的企业文化有哪...
点赞 评论 收藏
分享
ros275229:社团删了吧,cf因该1200才勉强入门吧,也删了,你可以写算法刷了多少道,都比这个好
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务