Instead of this:
class base
{
protected:
base( int value )
: member{value}
{}
int member = 0;
};
class derived_1 : public base
{
public:
derived_1()
: base{ 1 }
{}
};
class derived_2 : public base
{
public:
derived_2()
: base{ 2 }
{}
};
This would be useful:
class base
{
protected:
int member = 0; // Default value
};
class derived_1 : public base
{
base::member = 1; // Instead of passing it to a base class constructor
};
class derived_2 : public base
{
base::member = 2;
};
Will c++1y support this, or similar, syntax?
No, there are currently no plans to allow this. It seems a bit odd to allow an initializer to bypass a base class constructor (if any); it would seem to make more sense to allow a base-class specifier to contain an initializer:
class derived_1 : public base = {1}
{
};
You might consider submitting a proposal, if you can explain how the language would benefit (do you have a concrete use case?).
As a workaround, you might consider using a class template:
template<int I = 0>
class base { protected: int member = I; };
class derived_1: public base<1> {};
If you need to preserve a common base class, use an intermediate base class template:
class base { protected: int member = 0; };
template<int I>
class base_init: public base { base_init() { base::member = I; } };
class derived_1: public base_init<1> {};
Not sure if it's relevant, but the rules on aggregates and aggregate initialization look likely to change in C++14: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3653.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With