I have a Visual Studio 2008 C++ project where I would like to heap allocate a struct and initialize it using an initializer list.
class Foo {
public:
explicit Foo( int a );
};
struct Bar {
Foo foo;
int b;
};
Bar a = Bar { Foo( 1 ), 2 }; // Works!
Bar* b = new Bar{ Foo( 1 ), 2 }; // Errors!
Is there a way to do this?
C++11 allows it, or something very similar. Since you’re using VC 2008,
however, this won’t help you. The only solution I know is to create a
local instance with the initialization, and then pass this: