【发布时间】:2018-10-11 21:32:42
【问题描述】:
我想创建一个方法schedule_function,它将指向BasicAlgo 对象的成员函数的指针保存到ScheduledEvent,但没有在BasicAlgo 的父类Strategy 中定义所述函数.现在我正在使用这种方法,它可以很好地保存Strategy 中的函数,但不适用于BasicAlgo 函数:
class Strategy {
schedule_function(void (Strategy::*func)()) {
// Puts the scheduled event built with the strategy function onto a list
heap_eventlist.emplace_back(std::make_unique<events::ScheduledEvent>(func));
}}
我尝试将Strategy::*func 替换为Strategy*::*func,但这会导致编译器错误并且看起来不正确。
有没有办法将派生类BaseAlgo中的成员函数的指针作为参数在基类Strategy中,而不在Strategy中定义函数?
【问题讨论】:
标签: c++ pointers inheritance polymorphism member