mercredi 6 février 2019

c++: How do I maintain a statically defined list of objects implementing an interface to conditionally call at runtime?

I have an abstract base class, let's say MyInterface, that defines some interface with the following pure virtual functions:

virtual bool includeMe() = 0;
virtual void doSomething() = 0;

What I want to do is define at compile time a list of some objects that implement this interface and then at run-time I want to call the includeMe() method of all these objects and only if it returns true I want to then subsequently periodically call the doSomething() method of this set of objects. So I'm trying to figure out if I should have some statically defined std::vector<std::unique_ptr<MyInterface>> interface_implementations and then iterate through this vector once calling includeMe() on the object and if it returns false then remove it from the vector so later on I can just iterate through my interface_implementations vector which will only contain the objects I want to include. Is this the best way to do this?

Aucun commentaire:

Enregistrer un commentaire