0

if there is one object, we can initialize it with constructor in the following way -

class obj{
int x;

public:
obj(int n) {x=n;}
};

int main()
{
   obj p(2);
}

Is there an exact same way for array of object; and I mean an exact same way.

I know about other two way to initialize an object with constructor -

obj p = obj(2);
obj p = 2; // for one parameter constructor

and there equivalent way to initialize array of object -

obj p = {obj(1), obj(2), obj(3), obj(4)};
obj p[] = {1, 2, 3, 4};

but I failed to find a similar way for initializing array of object shown in the first code for a single object.

2 Answers2

0

If I understand your question right...

// Initialize array member varible - method 1
struct Foo
{
   int a[3] = {1, 2, 3};
};

// Initialize array member varible - method 2
struct Bar
{
   Bar() : a{1, 2, 3} {}
   int a[3];
};

// Can't do this.
struct Foo
{
   int a[] = {1, 2, 3};
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • did not understand the second method. – Syed Nakib Hossain Apr 28 '15 at 17:46
  • The second method is similar to initialize members that are not of type array. If you had a member `int b;`, you can initialize it with `b{20}` in the member initializer section of the constructor. – R Sahu Apr 28 '15 at 17:47
  • i did not know structure has constructor section :/ and what's the colon for? – Syed Nakib Hossain Apr 28 '15 at 17:52
  • @SyedNakibHossain, `struct` and `class` behave in very similar manner. The major difference is that the default access of `struct`s is `public` while the default access of `class` is `private`. – R Sahu Apr 28 '15 at 17:54
  • @SyedNakibHossain the colon is for the member initializer list: http://en.cppreference.com/w/cpp/language/initializer_list and http://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list/926795#926795 – evan Apr 28 '15 at 18:36
0

This will do it:

#include <array>
class obj{
  int x;

 public:
  obj(int n) {x=n;}
};

int main()
{
   std::array<obj,3> p = {3, 7, 11};
}

std::array gives you same semantics as a primitive array type but qualifies as a c++ container. You would probably be better off using std::vector, which offers dynamic re-sizing and more protection:

#include <vector>
...
std::vector<obj> p = {obj(1), obj(2), obj(4)};

The nice thing about using generic containers is you can often use them interchangably:

#include <vector>
//#include <array>
#include <algorithm>
...
int main()
{
   std::vector<obj> p = {obj(1), obj(2), obj(4)};     
   // or the following:
   // std::array<obj,3> p = {obj(1), obj(2), obj(4)};

   // either one of the above will work here
   std::for_each(begin(p), end(p), 
                [](obj const & o){ std::cout << o.x << " "; });
                std::cout << std::endl;
   //           ^^^ yes, this only works if x is public
}

Note that std::array is sized at compile time so it does require that you tell it the number of elements.

You could also rewrite the original class to use initializer list construction as follows:

class obj{
  int x;
 public:
  obj(int n) : x(n) { }
};

Even in this example it might help because it allows you to make x const. In general it has a lot of benefits and is a good habit to get into.

evan
  • 265
  • 2
  • 8