How do I declare an array of objects whose class has no default constructor?

后端 未结 13 1033
时光取名叫无心
时光取名叫无心 2020-11-30 06:25

If a class has only one constructor with one parameter, how to declare an array? I know that vector is recommended in this case. For example, if I have a class



        
相关标签:
13条回答
  • 2020-11-30 07:02

    If it makes sense for your class, you could provide a default value for your constructor parameter:

    class Foo
    { 
    public: 
      explicit Foo(int i = 0); 
    }
    

    Now you have a default constructor. (A "default constructor" is a constructor that can be called with no arguments: FAQ)

    I'd also recommend making your constructor explicit, as I did above. It will prevent you from getting Foos from ints when you don't want request them.

    0 讨论(0)
提交回复
热议问题