0

In C++14, I'm deriving from a struct:

struct base {
  int i;
};

struct derived: base {};

I can initialize base:

base b{10};

But I can't do the same with derived:

derived d{10};

This is working in C++17, but with earlier compilers seems invalid. How can I initialize base fields from derived?

It seems the compiler is looking for the copy-constructor, which is obviously invalid here.

Daniel
  • 2,318
  • 2
  • 22
  • 53
  • 1
    My guess is that in C++14, `derived` is not an aggregate, while in C++17 it is. – Daniel Langr Oct 25 '22 at 08:31
  • 1
    Dupe: [Changes in C++17 Aggregates](https://stackoverflow.com/a/50786105/12002570) – Jason Oct 25 '22 at 08:36
  • Here is *exact dupe*: [List-Initialization of a Subclass](https://stackoverflow.com/questions/73209232/list-initialization-of-a-subclass) – Jason Oct 25 '22 at 08:39
  • The question is focusing on *what to do with C++14*. Moreover, in case having more base members and derived members, writing n*m constructors for derived is overwhelming. Maybe move to template-based approach? – Daniel Oct 25 '22 at 08:48

0 Answers0