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

前端 未结 10 923
太阳男子
太阳男子 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条回答
  •  温柔的废话
    2020-11-28 05:20

    Just another way to disallow copy constructor,For convenience, a DISALLOW_COPY_AND_ASSIGN macro can be used:

    // A macro to disallow the copy constructor and operator= functions
    // This should be used in the private: declarations for a class
    #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
      TypeName(const TypeName&) = delete;      \
      void operator=(const TypeName&) = delete
    

    Then, in class Foo:

    class Foo {
     public:
      Foo(int f);
      ~Foo();
    
     private:
      DISALLOW_COPY_AND_ASSIGN(Foo);
    };
    

    ref from google style sheet

提交回复
热议问题