My parser is creating an abstract syntax tree, composed of NodeSomething structs which inherit from an empty Node base struct. These are stored in a std::list<Node>
.
My issue is that I don't want to write constructors for every single NodeSomething, and since the structs all inherit from a base class they are no longer aggregates, and as such I can't use initaliser lists (brace initalisation).
Since all of these structs are fairly simple, mostly containing one or two int or string variables, it seems very convoluted to have to write basic constructors for all of them. I'd remove the need for inheritance, but can't see any better way to create a generic list of a variety of these Node subclasses.
Anyway here's some trimmed code samples to explain what I mean:
struct Node {};
struct NodePerson : Node
{
std::string name;
int age;
};
struct NodeVariable : Node
{
int val;
};
And then in the implementation something like this:
std::list<Node> tree;
tree.push_back(NodePerson {"Paul", 23});
This will spit out an no matching constructor for initalization of 'NodePerson'
error, and then 3 notes about argument mismatch (2 given 1 expected) for the implicit move, copy, and default constructors.
From what I understand this is expected behaviour since the structs aren't aggregates anymore and can't use brace initalisation, but having to write constructors for each of them seems very unweildly. Is there a simple solution to this?
Edit: Since I didn't make it clear Node
is intended to be an abstract class used only for inheritance, so that I can store a list of different Node types.