Object array initialization without default constructor

后端 未结 11 2106
误落风尘
误落风尘 2020-11-22 04:20
#include 
class Car
{
private:
  Car(){};
  int _no;
public:
  Car(int no)
  {
    _no=no;
  }
  void printNo()
  {
    std::cout<<_no<

        
11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-22 05:04

    You can create an array of pointers.

    Car** mycars = new Car*[userInput];
    for (int i=0; i

    or

    Car() constructor does not need to be public. Add a static method to your class that builds an array:

    static Car* makeArray(int length){
        return new Car[length];
    }
    

提交回复
热议问题