T
toton
Hi,
I can initialize an array of class with a specific class as,
class Test{
public:
Test(int){}
};
Test x[2] = {Test(3),Test(6)}; using array initialization list. (Note
Test do NOT have a default ctor).
Is it possible to do so in the class parameter initialization using
specific ctor?
like
class TestContainer{
private:
Test _x;
public:
TestContainer(int size) : _x(Test(size) {}
};
This works.
class TestContainer{
private:
Test _x[2];
public:
TestContainer(int size) : ??? {} //how to do it like array
initialization?
};
In actual case Test class is a specific kind of semi-fixed-size
container (not defined in STL! ) which needs a size parameter. The
class like TestContainer holds such Test class two instance (and only
two). I know that an alternative is to store Test _x1, Test _x2; and in
initialization list TestContainer(int size) : _x1(Test(size) ,
_x2(Test(size)){} , or in general case, using an STL vector with
reserve space 2. Or storing a pointer instead of the object itself.
But my question is, In object initializer list can a array
initialization be called? If yes, what is the syntax?
Thanks
abir
I can initialize an array of class with a specific class as,
class Test{
public:
Test(int){}
};
Test x[2] = {Test(3),Test(6)}; using array initialization list. (Note
Test do NOT have a default ctor).
Is it possible to do so in the class parameter initialization using
specific ctor?
like
class TestContainer{
private:
Test _x;
public:
TestContainer(int size) : _x(Test(size) {}
};
This works.
class TestContainer{
private:
Test _x[2];
public:
TestContainer(int size) : ??? {} //how to do it like array
initialization?
};
In actual case Test class is a specific kind of semi-fixed-size
container (not defined in STL! ) which needs a size parameter. The
class like TestContainer holds such Test class two instance (and only
two). I know that an alternative is to store Test _x1, Test _x2; and in
initialization list TestContainer(int size) : _x1(Test(size) ,
_x2(Test(size)){} , or in general case, using an STL vector with
reserve space 2. Or storing a pointer instead of the object itself.
But my question is, In object initializer list can a array
initialization be called? If yes, what is the syntax?
Thanks
abir