Hello everyone I need some help. I am trying to make a class that will store a class with an unknown number and type of arguments and I am stuck on one part. I need to call method Click() from the ControlItem class which is inherited from ControlItemBase class. I know that I can NOT use 'virtual function' in this case, it just doesn't work, so I am stuck on this part. I tried to do different casts but that didn't work.
I also found this question Storing a variadic template class into std::vector the guy who answered made almost the same implementation of the code as I did, but he didn't explain how to call the methods in this case.
I hope someone can help me solve this problem, thank you.
class ControlItemBase {
public:
void OnClick(){
// HOW TO ACCESS THE ControlItem -> Click() HERE
}
};
template<class... Args>
class ControlItem : public ControlItemBase{
public:
std::tuple<Args...> arguments;
std::function<void(Args...)> func;
ControlItem(std::function<void(Args...)> func) : func(func){}
void Click() {
// Need to call this method
}
void SetArgs(std::tuple<Args...> args) {
arguments = args;
}
};
class Controls
{
public:
static std::vector<ControlItemBase> items;
Controls();
~Controls();
template<class Class, class... Args>
void AddEventListener(Class* context, void (Class::* func)(Args...), Args... args) {
ControlItem<Args...> c(std::bind(func, context, args...));
c.SetArgs(std::make_tuple(std::move(args...)));
items.push_back(c);
}
};