1

We can initialize an array for a structure which has a default constructor, but can we do that with a parametrized constructor?

struct class{
   int room;
   int floor;
   class(){
    room=0;
    floor=0;
  }
};

int main(){
   class c1[5];
}

Above code works fine. But, what to do when there is a parameterized constructor?

struct class{
  int room;
  int floor;
  class(int r,int f){
    room=r;
    floor=f;
  }
};

1 Answers1

1

You can use list initialization:

struct test {
  test(int,int);
};

test c1[] = {
    {1, 2},
    {3, 4},
    {5, 6},
};
eerorika
  • 232,697
  • 12
  • 197
  • 326