jeromintus
jeromintus

Reputation: 481

Template and function pointer

Is it somehow possible to store the class from a template without making the the whole class a template?

Task:

I have two functions, v1 without parameters and v2 with parameters, If v1 was called somewhere nothing happens with Use(), if v2 was called somewhere Use() should execute a function_ptr with the instance I got from DoSometh(T*).

e.g.

    class MyClass
    {
      //v1 no parameters
      void DoSomething()
      {
      }

      //v2 with parameter
      template<class T>
      void DoSomething(T* instance, void (T::*func)())
      {
        store somewhere?? = instance;
      }

      void Use()
      {
        //if DoSometh(T* instance) was used before
        if(instance != NULL)
        {
            (*instance->)//call function pointer from DoSomething(T*,void  (T::*)())
        }
      }
    }

std::function problem
update:


class Timer : public ITickable
{
  std::function<void()> test; //adding this does weird things

  virtual void Tick() {}
}

class MyClass
{
   ITickable* tickable_; 

void Tick()
{
    tickable_->Tick(); //let's assume it points to a Timer obj.
}


}

Upvotes: 0

Views: 108

Answers (1)

JorenHeit
JorenHeit

Reputation: 3997

I think std::function and std::bind (C++11) do accomplish what you want, as already suggested in the comments. A simplified mock-up of your Timer class could be:

class Timer
{
    std::function<void()> m_task;

public:
    template <typename T>
    void setTask(T &instance, void (T::*fcn)()) // consider T const & if applicable
    {
        m_task = std::bind(fcn, &instance);
    }

    void fire()
    {
        if (m_task) // std::function overloads operator bool()                                                                          
            m_task();
    }
};

When setTask is called with an object and a member-function that can be called on this object, a std::function object is created (you could choose to do this in a constructor of course). When the timer fires, this object is checked (using operator bool(), provided by std::function), and if it is callable (e.g. when setTask() has been called before), it calls the function.

For example:

class MyClass
{
public:
    void func()
    {
        std::cout << "Hi from MyClass\n";
    }
};

class MyOtherClass
{
public:
    void func()
    {
        std::cout << "Hi from MyOtherClass\n";
    }
};


int main(int argc, char **argv)
{
    MyClass x1;
    MyOtherClass x2;

    Timer t1, t2;
    t1.setTask(x1, &MyClass::func);
    t2.setTask(x2, &MyOtherClass::func);

    t1.fire();
    t2.fire();
}

Upvotes: 1

Related Questions