Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: Constructor versus initializer list in struct/class

An object of a struct/class (that has no constructor) can be created using an initializer list. Why is this not allowed on struct/class with constructor?

struct r { int a; };
struct s { int a; s() : a(0) {} };
r = { 1 }; // works
s = { 1 }; // does not work
like image 879
Anycorn Avatar asked Jan 19 '10 18:01

Anycorn


1 Answers

No, an object with a constructor is no longer considered a POD (plain old data). Objects must only contain other POD types as non-static members (including basic types). A POD can have static functions and static complex data members.

Note that the upcoming C++ standard will allow you to define initializer lists, which will allow non-POD objects to be initialized with braces.

like image 129
Collin Dauphinee Avatar answered Sep 17 '22 22:09

Collin Dauphinee