1

This works...

struct FOO
{
  char bar1[50 + 1];
  char bar2[50 + 1];
};

FOO foo[] = 
{
  {"baz1", "baz2"},
  {"baz3", "baz4"}
};

But this does not...

struct FOO2
{
  FOO2(void) { };
  char bar1[50 + 1];
  char bar2[50 + 1];
};

FOO2 foo2[] = 
{
  {"baz1", "baz2"},
  {"baz3", "baz4"}
};

Morover, this...

struct FOO3
{
  void init(void) { };
  char bar1[50 + 1];
  char bar2[50 + 1];
};

FOO3 foo3[] = 
{
  {"baz1", "baz2"},
  {"baz3", "baz4"}
};

...works too.

So, methods don't disallow array initialization but a constructor does.

From Microsoft's error description these attributes (among others) cause an entity to become a "non-aggregate" and therefore no longer a valid target for array initialization:

Constructors
Private or protected members
Base classes
Virtual functions 

Why, in the case of constructors?

emptyd
  • 11
  • 1
  • With no special knowledge of this(except for reading [this](http://en.cppreference.com/w/cpp/language/aggregate_initialization)), I would say that presumably it's because if your constructor modifies the arrays as well, the runtime can't guarantee things will be consistent. If the constructor is setting bar1 to something, and the initalizer is setting it to something else, what should happen? This answer seems like good reading too : http://stackoverflow.com/questions/35891233/initializing-a-struct-with-aggregate-initialization-and-member-initializers – Karl Reid May 05 '17 at 17:12

0 Answers0