How do I make this C++ object non-copyable?

前端 未结 10 977
太阳男子
太阳男子 2020-11-28 04:36

See title.

I have:

class Foo {
   private:
     Foo();
   public:
     static Foo* create();
}

What need I do from here to make Fo

10条回答
  •  猫巷女王i
    2020-11-28 05:14

    In C++11, you can explicitly disable the creation of default copy and assignment constructor by placing = delete after the declaration.

    From Wikipedia:

    struct NonCopyable {
        NonCopyable() = default;
        NonCopyable(const NonCopyable&) = delete;
        NonCopyable & operator=(const NonCopyable&) = delete;
    };
    

    The same goes for classes of course.

提交回复
热议问题