I need to dynamically create instances of an Aggregate
in C++ which inherits from multiple base classes, where the combination of base classes can vary based on runtime conditions. I want to avoid pre-populating all possible combinations.
Here’s a simplified version of what I’m trying to achieve
I have several base classes:
class CharBase
{
CharBase() { std::cout << "CharBasen"; }
};
class UnsignedCharBase
{
UnsignedCharBase() { std::cout << "UnsignedCharBasen"; }
};
class ShortBase
{
ShortBase() { std::cout << "ShortBasen"; }
};
class IntBase
{
IntBase() { std::cout << "IntBasen"; }
};
I use the following Aggregate
class template to unherit from multiple base classes
template<typename... Bases>
class Aggregate : Bases…
{
Aggregate() { std::cout << "Aggregaten"; }
};
My goal is to create instances of the Aggregate
classes on demand based on runtime conditions. The expected result is to create an Aggregate
instance as if I hardcoded it, for example:
new Aggregate<CharBase, UnsignedCharBase, ShortBase>
new Aggregate<UnsignedCharBase, IntBase>