List of initializers for a heap-allocated structure

I have a Visual Studio 2008 C ++ project where I would like a bunch of highlighting a struct and initializing it with 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?

+3


source to share


3 answers


C ++ 11 allows this, or something very similar. Since you are using VC 2008 however this will not help you. The only thing I know is to create a local instance with initialization and then pass this:



Bar forInitialization = { Foo( 1 ), 2 };
Bar* b = new Bar( forInitialization );

      

+1


source


Try

Bar* b = new Bar({ Foo( 1 ), 2 });

      



Disclamer: Checked with GCC only -std=c++0x

.

+2


source


Try adding a constructor to your structure, it will be called when called new

0


source







All Articles