更新时间:2022-05-09 01:26:13
一种简单的方法是定义表示函数的单个接口类型:
A simpler approach would be to define a single interface type representing a function:
struct ICallback
{
virtual bool operator()() const = 0;
};
并根据需要实施多次:
struct Foo : ICallback
{
virtual bool operator()() const { return true;}
};
struct Bar : ICallback
{
virtual bool operator()() const { return false;}
};
然后您的总线实现可以使用回调接口:
then your bus implementation can take callback interfaces:
class BusImplemantation{
public:
void addRequest(const ICallback* callback) { .... }
};
然后
BusImplemantation bus;
Foo foo; // can be called: bool b = foo();
Bar bar; // can be called: bool b = bar();
bus.addRequest(&foo);
bus.addRequest(&bar);
您也可以使用 std :: function 并完全避免使用通用界面。
You could also investigate using std::function and avoiding the common interface altogether.