How to delete the default constructor?

后端 未结 4 754
耶瑟儿~
耶瑟儿~ 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:46

    I would say make it private.. something like

    class MyClass
    {
    private:
        MyClass();
    }
    

    and no one(from outside the class itself or friend classes) will be able to call the default constructor. Also, then you'll have three options for using the class: either to provide a parameterized constructor or use it as a utility class (one with static functions only) or to create a factory for this type in a friend class.

提交回复
热议问题