c++ - 创建一个返回泛型成员函数指针的虚函数
问题描述
我想写一个COMMUNICATOR
具有虚函数的抽象类get_func()
。
class COMMUNICATOR : public QObject{
public:
virtual <what type???> get_func() = 0;
};
现在假设INT_SENDER
, INT_RECEIVER
, STR_SENDER
, 并且STR_RECEIVER
都派生自COMMUNICATOR
。它们每个都提供了自己的 get_func() 实现,它们可以像这样使用:
COMMUNICATOR *int_sender = new INT_SENDER;
COMMUNICATOR *int_receiver = new INT_RECEIVER;
COMMUNICATOR *str_sender = new STR_SENDER;
COMMUNICATOR *str_receiver = new STR_RECEIVER;
//INT_SENDER::get_func() and INT_RECEIVER::get_func() both return a member function pointer of signature void(*)(int)
connect( int_sender, int_sender->get_func(), int_receiver, int_receiver->get_func() );
//STR_SENDER::get_func() and STR_RECEIVER::get_func() both return a member function pointer of signature void(*)(std::string)
connect( str_sender, str_sender->get_func(), str_receiver, str_receiver->get_func() );
我想通过这个设计实现的是:
(1) 为所有派生自 COMMUNICATOR 的类赋予相同的接口:get_func() 函数
(2) 使用 COMMUNICATOR 及其派生类不需要处理 get_func() 返回的函数的签名。相反,函数指针直接传递给 connect();
是否有某种 hack 来实现我想要的,比如让 get_func() 返回一些类型擦除包装对象,我可以从中将函数指针转储到 connect() 中?还是我在这里尝试不可能的事情?
解决方案
推荐阅读
- postgresql - 带有 Postgresql 和计算字段的可更新视图
- python - 不在html模板django中显示模型值
- asp.net-core - 我如何访问我的 asp.net 核心 MVC 中的注册过程
- apache-flink - Flink1.10 为什么 DDL 不支持创建视图和创建函数?
- c# - 如何解决 Angular8 中的输入字符串格式不正确?
- c# - 如何使用 Entity Framework Core 从子表列表的每个订单中获取产品行?
- python - 管道密码以在批处理文件中提示
- python - 通过指定组 ID 将列表分成组
- react-native - React Native 中的不变违规:文本字符串必须在
零件 - c++ - boost::mpi 作为模板类的静态成员