سؤال

struct base {
};

//struct derived { // <--- this one works
struct derived : public base { // <--- but this one doesn't
  double x;
};

main () {
  derived d{0.5};
  return 0;
}

What is the way to initialize derived using braces, meaning without explicitly writing a constructor?

هل كانت مفيدة؟

المحلول

Only aggregates may be initialized this way (without defining your own constructor that is) and one of the requirements to be an aggregate is to not have any base classes. In short, you can't do that.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top