Object array initialization without default constructor

后端 未结 11 2104
误落风尘
误落风尘 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:51

    My way

    Car * cars;
    
    // else were
    
    extern Car * cars;
    
    void main()
    {
        // COLORS == id
        cars = new Car[3] {
            Car(BLUE),
                Car(RED),
                Car(GREEN)
        };
    }
    

提交回复
热议问题