In my code I have two almost identical structs, which I have here simplified:
struct foo {
struct bar {
int b;
} a;
};
struct foo2 {
struct qux {
int b;
int c;
} a;
};
(The only difference here is that qux has a member c while bar does not.)
My question is, Is there a way to let qux inherit from bar, without having to create two foo? This can easily be done with classes, but I want to know if it can be achieved with structs. In my imagination it would look something like this:
struct bar {
int b;
};
struct qux : bar {
int c;
};
struct foo {
bar_or_qux a;
};
foo? When you then writefoo f;, should it mean foo-with-c or foo-without-c?structs in C++ support all the features thatclasses do, so whateverclasssolution you're thinking of will work equally well for astruct.barand aquxare totally different in terms of their purpose in your program, then inheritance shouldn't be used.fooandfoo2? Are you yet to discover polymorphism?struct foo { std::unique_ptr<bar> a; };Are you yet to discover templates?template <typename T> struct foo { T a; };.