lundi 27 avril 2020

Automated call to class static functions C++11

Hello,

I'm working on a project executed on an ESP32 micro controller (c++11) and I would like to implement a class method that could take Class References (not instances of those classes) as arguments in order to execute static methods of those classes.

I manage to implement the following working solution :

#include <iostream>

    class Model
{
public:
    static void foo()
    {
        std::cout << "A.foo invoked" << std::endl;
    };
};

class ModelA: public Model
{
public:
    static void foo()
    {
        std::cout << "ModelA.foo invoked" << std::endl;
    };
};

class ModelB: public Model
{
public:
    static void foo()
    {
        std::cout << "ModelB.foo invoked" << std::endl;
    };
};


class Manager
{
public:

    template <class T = Model>
    void callFoo()
    {
        T::foo();
    }

    template<class T = Model, class ... args>
    void setup()
    {
       callFoo<T>();
       callFoo<args...>();
    }

};


int main()
{

    Manager manager;
    manager.setup<ModelA, ModelB>();

    return 0;
}

The output meets the expected result :

ModelA.foo invoked

ModelB.foo invoked

But I have the feeling that this approach is at best a poor hack...

Does someone has a better way to implement this (ideally without using a template) ?

Thank you in advance.

Vincent.

Aucun commentaire:

Enregistrer un commentaire