jeudi 3 août 2017

Populate static class member in subclasses

Here are 2 classes, a parent and a subclass:

class Model
{
    protected:
    static std::list<char *>   list;
}

class SubModel : public Model
{
    // Should add itself to Model::list...
}

So, here are 2 example classes. I would like to create a generic model in a way every subclass (correctly implemented) would register themselves to the static Model::list. That way, any other subclass is able to identify other "existing" models (and type names).

I've tried many ways, but I have no clue how to populate a static member in many other files only once.

Any idea, if it's possible?

EDIT: I'm looking for a way to do something like Model::list.push_back("SubModel"); somewhere in each sub model (with the according name of course)

Aucun commentaire:

Enregistrer un commentaire