In my program I have an abstract class from which several classes inherit. Each child class introduces a member variable to store data. I noticed that when attempting to initialize the child class, whether using aggregate initialization or initializer list, I get errors, shown below.
struct FooBase {};
struct Foo : FooBase { int value; };
int main()
{
Foo f = {8}; // "initializer for aggregate with no elements requires explicit braces"
Foo f{8}; // same error as above
}
I figured this is because Foo inherits the constructors from FooBase, but I have a couple questions about the behavior.
- Since the type is specified at compile time, why doesn't the aggregate constructor of
Foo
take precedence? - Is there a way to force precedence of the default constructor so that above initialization is possible?
- (Bit more broad) In general, how are (standard and aggregate) constructors of classes passed on to their children?
As I understand, the options would be setting the data after initialization (or making a setter method) or explicitly defining a constructor for Foo
. However, specifically in the context of the last question, what happens with move and copy constructors? (Is there good practice for ensuring classes are well behaved under inheritance?)