Object array initialization without default constructor

后端 未结 11 1929
误落风尘
误落风尘 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 04:58

    One way to solve is to give a static factory method to allocate the array if for some reason you want to give constructor private.

    static Car*  Car::CreateCarArray(int dimensions)
    

    But why are you keeping one constructor public and other private?

    But anyhow one more way is to declare the public constructor with default value

    #define DEFAULT_CAR_INIT 0
    Car::Car(int _no=DEFAULT_CAR_INIT);
    

提交回复
热议问题