How to delete the default constructor?

后端 未结 4 784
耶瑟儿~
耶瑟儿~ 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条回答
  •  Happy的楠姐
    2021-02-02 06:56

    Additionally to declaring the default constructor private, you could also throw an exception when somebody tries to call it.

    class MyClass
    {
      private:
        MyClass() 
        {
          throw [some exception];
        };
    }
    

提交回复
热议问题