jeudi 30 juillet 2020

Initialize member array of user defined objects in the constructor in C++

How do I initialize member array in my constructor? The member array is an array of user-defined objects(classes). Also, the number of elements in the array could be large more than 100, so I do not prefer to use initializer-list available in C++11 (unless there's some better way)

For instance, refer following code:

class Foo {
private:
   void *a;
   int b;
public:   
   Foo(void *, int);
   ~Foo();
}

class Bar {
private:
    Foo mObj[150];
public:
    Bar();
    ~Bar();
};

Bar::Bar() {
// ???
}

For sake of simplicity, assume I would like to initialize the members as follows: let's say member int b stores Sr. no & void *a store null ptr as of now... so mObj[0] = {0, nullptr}, mobj[1] = {1, nullptr} & so on..

  1. What should my Bar::Bar() constructor be like?
  2. Do I need to have Default const, Copy const & operator= for Foo()?
  3. Having Vector instead of an array a better option?

Aucun commentaire:

Enregistrer un commentaire