Home Good way of encapsulating member function pointers for a task manager
Reply: 0

Good way of encapsulating member function pointers for a task manager

user5391
1#
user5391 Published in September 21, 2018, 8:50 am

I would like to store a member function pointer and a related object pointer in a template class task. Then I want to make an array and call these member functions in the scheduler class.

However, if I save such tasks in an array, they all need to have the same template type. This obstacle makes saving the object pointer in the task class useless.

Now, I wonder whether there is a smart way to safe an array of such pairs in the related scheduler class. If I use a std::tuple, I cannot iterate at run-time through the container, or at least not in a very readable manner. Is there a solution, where I don't need a template for the scheduler class?

template <class Obj>
struct task {    
    typedef void (Obj::*task_fn_t)();
    Obj*        obj_ptr;  // object referring
    task_fn_t   function; // member function
};

template <class Obj>
class scheduler {
    task <Obj> *_tasks; // problem :(
};
share|improve this question

2 Answers 2

active oldest votes
up vote 7 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.313637 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO