pingu
pingu

Reputation: 8827

Instantiate and initialise a struct dynamically

I am initialising a c style struct using a list:

FooBar fb = { 12, 3.4 };

this works fine, but how would i create the struct dynamically using this curvy brace notation?

thanks

Upvotes: 1

Views: 1754

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254451

In C++11,

std::unique_ptr<FooBar> fb {new FooBar {12, 3.4}};

In C++03, you can't.

Upvotes: 3

Pubby
Pubby

Reputation: 53037

C++11:

FooBar* fb = new FooBar{ 12, 3.4 };

You can also use it in containers:

std::vector<FooBar> v;
v.push_back({ 12, 3.4 });

Upvotes: 3

Related Questions