I have a problem in a library I am developing. The problem is that, for what I designed, I need to call a pure virtual function from the constructor. But I need to avoid undefined behavior.
This is the design I have a class shape
that needs to be used as a sort of interface:
class shape
{
public:
shape(std::shared_ptr<configuration> conf)
{ generate(); }
std::shared_ptr<generated> mesh;
protected:
virtual void generate() = 0;
};
The user, as far as I have designed, will write a class that will actually create the mesh. My idea, right now not working, is to force calling the generate
method without the user's intervention.
In other words, the construction of the derived class must create the mesh. If I leave it to the user, the explicit call to generate
could be forgotten and will result in some weird and destructive behavior that won't be noticed until really later (this should never happen for this library). I was thinking also of using CRTP or something from TMP but I cannot devise anything that is similar in simplicity to the above code.
How can I design the base class and eliminate the possibility of forgetting one call?
Any suggestions are more than welcome!
Aucun commentaire:
Enregistrer un commentaire