Constructor initialising an array of subobjects?
Posted
by ojw
on Stack Overflow
See other posts from Stack Overflow
or by ojw
Published on 2010-04-22T15:18:05Z
Indexed on
2010/04/22
15:23 UTC
Read the original article
Hit count: 146
c++
|constructors
Say I have several objects within a class, each of which needs constructing with a different value. I can write something like this:
class b
{
public:
b(int num)
{
// 1 for a.b1, and 2 for a.b2
}
};
class a
{
public:
b b1;
b b2;
a() : b1(1), b2(2)
{
}
};
However, is it possible to do the same thing if those multiple objects are stored in an array?
My first attempt at it doesn't compile:
class a
{
public:
b bb[2];
a() : bb[0](1), bb[1](2)
{
}
};
© Stack Overflow or respective owner