How to delete the default constructor?

后端 未结 4 761
耶瑟儿~
耶瑟儿~ 2021-02-02 06:00

Sometimes I don\'t want to provide a default constructor, nor do I want the compiler to provide a system default constructor for my class. In C++ 11 I can do thing like:

4条回答
  •  自闭症患者
    2021-02-02 06:42

    Since c++11, you can set constructor = delete. This is useful in conjunction with c++11's brace initialization syntax {}.

    For example:

    struct foo {
      int a;
      foo() = delete;
    };
    
    foo f{}; // error use of deleted function foo::foo()
    foo f{3}; // OK
    

    see https://en.cppreference.com/w/cpp/language/default_constructor#Deleted_implicitly-declared_default_constructor

提交回复
热议问题