通过函数指针定义类范围之外的方法
Defining methods outside of a class's scope via function pointers
前言
我正在尝试创建一个事件系统。默认情况下,事件不执行任何操作。如果一个对象需要选择加入事件系统,那么只需要程序员为侦听器定义一个可调用的函数。
从理论上讲,这听起来很棒,但实际上我不确定是否可以在C++内实现这种事情。下面是我想象的代码外观的示例。
大纲
#include <iostream>
enum Color{ red, green, blue };
class Event {
public:
// Would be called within the context of a loop.
// Each time we go through the loop, we would have different input.
void listen(const Color& color) {
// If the callable is defined, then call it.
if(callable) callable(color);
}
// We would pass in function to be called within listen()
void defineAction(void(*function)(const Color &color)) {
callable = function;
}
private:
void (*callable)(const Color& color) = nullptr;
};
class Greeter: public Event {
void greet() { std::cout << "Hello" << std::endl; }
};
int main(int argc, char** argv) {
Greeter greeter;
// This doesn't work for two reasons. We cannot define a function here,
// and scoping rules wont allow for an implicit this->greet().
// Ideally, I would like to do this inline with a lambda
/*void greetIfRed(const Color& color) {
if(color == red)
// call greet().
}
greeter.defineAction(&greetIfRed);
*/
}
扩展这个想法
基本上,Event 类将收到一些东西,每次我们经历事件循环时,这些内容都会发生变化。我为此示例选择了颜色,但它可以是鼠标坐标、整数或其他内容。该类型需要事先严格定义。
在我的设计中,我不希望事件循环中的类无关紧要。我们可以有一个迎宾员,一个球员,或者只是说再见的东西。我想事件循环看起来像这样。(伪代码(
while(event) {
greeter.listen(event)
player.listen(event)
text.listen(event) // e.g.,the text could change color if the mouse is over it,
}
问题
是否可以通过利用该类方法的函数指针定义类范围之外的方法?这需要为子类工作。类Greet
和函数greetIfRed
捕获我正在尝试实现的功能。
因此,我尝试了许多建议,并设法创建了一个没有进行太多更改的工作示例。我用 C++14 编译了这个。
#include <iostream>
#include <functional>
enum Color{ red, green, blue };
class Event {
public:
void listen(const Color& color) {
if(callable) callable(color);
}
void defineAction( std::function<void(const Color& color)> function) {
callable = function;
}
protected:
std::function<void(const Color& color)> callable = nullptr;
};
class Greeter: public Event {
public:
void greet() { std::cout << "Hello" << std::endl; }
};
int main()
{
Greeter greeter;
greeter.defineAction([&greeter](const Color& color){
if(color == red)
greeter.greet();
});
greeter.listen(red);
greeter.listen(green);
}
相关文章:
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- 通过函数指针定义类范围之外的方法
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 在 c++ 中解决段树以外的范围查询的有效方法是什么?
- C++中静态方法的局部变量范围
- 在 C++20 计算范围内相邻对的最简洁明了的方法是什么?
- 使用范围解析运算符时,在构造函数中调用虚拟方法是否安全?
- 是否可以在基于范围的 for 循环中使用模板化的开始/结束方法
- 是否有一种方法可以根据派生的可能性值范围限制对象参数
- C 将方法和字段带入外部范围,而无需明确的参考
- 是否有一种方法可以避免标头文件中使用的constexpr函数输入全局范围,而无需额外的名称空间
- 使用范围V3视图来实现begin()/end()方法
- 在嵌套名称空间范围中使用名称空间的正确方法
- 使用其范围版本从容器中删除元素的最佳方法
- C++ |方法范围指针管理
- 用辅助方法范围范围的问题来操纵字符串
- Java,这是可能的吗?自动方法范围跟踪,就像C++中的宏一样
- 游戏循环中的可变内存方法范围
- 正在从方法范围复制char*
- 从c++调用qml - javascript中的方法.范围的问题