So, I'm making a library for a little D2D Engine; but that's not the point, the thing is, I have this class which will be the base class the user's class will inherit. My main idea would be something like this:
struct BaseEngine {
// I have two pure virtual functions so the user has to define them.
virtual bool onLoad() = 0;
virtual bool onFrame() = 0;
};
Now, if it all were to be in the same project, I could do something like this right after that:
struct Derived : public BaseEngine {
bool onLoad() override;
bool onFrame() override;
};
const std::unique_ptr<BaseEngine> app = std::make_unique<Derived>();
But my idea is, in fact, to not hold the derived class in my header files, and build the library without any possible definition of a derived class, thus, the user can just name it whatever they want in their project.
Of course it won't let me compile it, because I can't construct BaseEngine
because it has pure virtual functions.
Then I though of somehow using templates to maybe solve this issue? Now I'm not very familiar with templates, but my idea was to make something like:
std::unique_ptr<BaseEngine> app;
template<class T : public BaseEngine>
void instantiator() {
app = std::make_unique<T>();
}
Knowing that T
holds an implementation for onLoad()
and onFrame()
. But of course when I need a feature such as templates of explicitely derived classes, no feature exists (not that I know of, at least).
My main question being: is there a way for me to initialize an object from an "unknown" derived class of my known base class?
Aucun commentaire:
Enregistrer un commentaire