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

后端 未结 13 1113
时光取名叫无心
时光取名叫无心 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 06:52

    class single
    {
        int data;
    public:
        single()
        {
            data = 0;
        }
        single(int i)
        {
            data = i;
        }
    };
    
    // in main()
    single* obj[10000];
    for (unsigned int z = 0; z < 10000; z++) 
    {
        obj[z] = new single(10);
    }
    

提交回复
热议问题