If A
is copy constructable (or move constructable, but that's not relevant since you're not using C++11), then you can do
class B {
private:
A a;
static const TypeX x;
static const TypeY y;
static A initA(bool useTypeX)
{
if (useTypeX)
return A(x);
else
return A(y);
}
public:
B(bool useTypeX)
: a(initA(useTypeX))
{}
};
Edit: Of course, thinking about it, this is just as legal, eliminating the need for an extra function. The key is that you're explicitly creating them. See https://ideone.com/yUIF0Z
B(bool useTypeX) : a( useTypeX ? A(x) : A(y) ) {}