0

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?
9
  • 1
    "Having Vector instead of an array a better option?" -- Yes!! Commented Jul 30, 2020 at 13:04
  • 2
    Initialize to what? Commented Jul 30, 2020 at 13:06
  • 1
    @FredLarson not necessarily. Not enough information is given to jump to this conclusion. Commented Jul 30, 2020 at 13:06
  • @SergeyA Initialize with valid values. For instance, 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.. Commented Jul 30, 2020 at 13:09
  • 1
    @SergeyA: I think vector (or std::array, or other container) should always be preferred over old-fashioned array unless there is a compelling argument otherwise. Commented Jul 30, 2020 at 13:09

1 Answer 1

1

From the comments, it seems like the ultimate task at hand is to call Foo constructor for each element of the array with arguments of (i, nullptr), where i is increased on each iteration from 0 to N.

Here is the code which would do this with std::index_sequence. This was only introduced with C++14, so if you are not using it, you will have to do implement it yourself, which is not hard at all - let me know if you need help with that.

#include <array>
#include <utility>
#include <cstddef>

struct Foo {
    Foo(void*, int);
};

class Bar {
private:
    static constexpr size_t array_sz = 150;
    template<size_t... Ix> Bar(std::index_sequence<Ix...> ) :
        mObj{Foo{nullptr, Ix}...}     { }
    std::array<Foo, array_sz> mObj;
public:
    Bar();
    ~Bar();
};

Bar::Bar() : Bar(std::make_index_sequence<array_sz>{}) { }
Sign up to request clarification or add additional context in comments.

2 Comments

Note that OP tagged the question C++11, while std::index_sequence was introduced in C++14.
@eerorika this is a good point. It is quite easy to hand-rollindex_sequence in C++11, I will edit the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.