toton wrote:
Quote:
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?
You can't with arrays, but you can with std::vector (which you should
probably be using anyway,
http://www.parashift.com/c++-faq-lit...html#faq-34.1). For
instance:
#include <vector>
using namespace std;
template<typename T>
class Initializer
{
vector<Tv_;
public:
Initializer( const unsigned capacity=0 )
{
v_.reserve( capacity );
}
Initializer& Add( const T& t )
{
v_.push_back(t);
return *this;
}
operator vector<T>() const
{
return v_;
}
};
class Example
{
const vector<doublev_;
public:
Example( double d0, double d1, double d2 )
: v_( Initializer<double>( 3 )
.Add(d0)
.Add(d1)
.Add(d2) )
{}
// ...
};
Cheers! --M