- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个执行一组基本操作的程序,但允许用户填写调用的特定函数(他们在编译前选择这些函数)。例如,我的程序可能会调用函数 filter(input,&output)
但用户可以编写自己的过滤器。
我读到的可以解决这个问题的方法是函数指针和虚函数。看起来我可以按照
int (*pt2Filter)(float,&float) = NULL;
int IIRFilter(float input, float &output);
pt2Filter=&IIRFilter;
函数指针。但这并不能让我跟踪过滤器中的内部状态。
或者我可以上课 myClass
用虚拟 filter
函数,然后用户会生成一个 IIR
从 myClass
继承的类并覆盖 filter
功能。
class myClass
{
virtual void Filter(float input, float &output);
...
};
class IIR : public myClass
{
float stateVariable;
virtual void Filter(float input, float &output);
}
void IIR::Filter(float input, float &output)
{ //IIR filter done here }
我想我的问题是如何在不知道 IIR
的情况下从我的程序调用过滤器函数类甚至存在?
或者如果我的做法完全错误,我该如何调用我的 Filter
当我的目标是 1 时起作用:让用户定义他们想要的任何过滤器。 2:不允许用户更改我的源代码
更新 这可能没有我最初想象的那么困难。我创建了一个头文件,用户可以在其中说出他们希望 Filter 类使用以下行调用哪个函数
//User types this into "FunctionImplementations.h"
#include "IIR.h"
typedef IIR FilterImplementation;
//then I just type
#include "FunctionImplementations.h"
FilterImplementation.filter(); //Implements IIR classes filter function
最佳答案
有几种方法可以实现这种多态性。
主要问题是您需要编译时多态行为还是运行时多态行为。在第一种情况下,解决方案通常是定义一个函数(或类)模板来执行您的通用工作,并使用通用代码调用的可调用对象的类型对其进行参数化,以完成自定义部分的工作:
// This is how you would define your generic procedure
template<typename F> void do_something(F f, ...)
{
...
f(...);
...
}
// This is how you would use it...
void my_func(...) { ... };
do_something(&my_func, ...); // with a function pointer
do_something([] (...) { ... }, ...); // with a lambda
struct my_functor { operator void () (...) { ... } };
do_something(my_functor(), ...); // with a functor
如果定义自定义行为的对象类型仅在运行时确定,那么您有两种可能性:要么使用std::function<>
用于封装回调,或者使用虚函数方法。我个人更喜欢前者,因为它不会强制你为了实现动态多态性而创建继承层次。
这就是您使用 std::function<>
的方式对象:
void my_func1(int, int) { ... }
void my_func2(int, int) { ... }
std::function<void(int, int)> fxn = &my_func1;
fxn(2, 3);
...
fxn = &my_func2;
fxn(3, 4);
...
fxn = [] (int x, int y) { ... };
fxn(4, 5)
这就是您如何利用它来定义您的通用过程:
void do_something(std::function<void(int, int)> f, ...)
{
...
f(3, 4);
...
}
此时,您可以调用do_something()
任何可以分配给 std::function
的东西(即任何具有兼容签名的可调用对象)。
关于c++ - 如何使用函数指针或虚函数来允许另一个程序员定义函数的实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14464297/
在 PHP 中,开始破解并弄清楚页面上发生的事情非常容易。只需输入一堆回声和 print_r,仅此而已。看来这种技术在 python 中对我不起作用。我通过在 python 照片上传模块中进行黑客攻击
程序员/后端开发方向Java 跳槽注意事项(简历和面试经验分享) 应届生面试经验参考:https://www.cnblogs.com/rainbow-1/p/16779048.html 简历: 1、
上图的意思: 百战百胜,屡试不爽. 故事 程序员小张: 刚毕业,参加工作1年左右,日常工作是CRUD 。 架构师老李: 多个大型项目经验,精通各种开发架构屠龙宝术;
周末了开心一下 谈到程序员,对于外行人来说一贯的印象就是格子衫大裤衩外加人字拖,蓬头(秃头)垢面黑眼圈,还有就是“人傻钱多死得快”🤣,这是外界对程序员固有的思想,但是作为新世纪的程序员,我们可没那么
我从事C++编程已有5年了,现在我意识到知道如何“不同地”思考的值(value)。 这个问题适用于尝试其他编程语言并遇到过类似情况的C++程序员: “哇,这太酷了!我不知道我可以那样编程。” “哇,我
互联网已经渗入我们生活的方方面面,于是我们就免不了会思考:如果有一天网络断了怎么办呢? 英国 Cable.co.uk 曾进行了一项有趣的"断网"调查,调查发现在 2500
这些是 “程序员可见” x86-64 寄存器: (来源:usenix.org) 隐形寄存器呢?刚刚才知道MMU寄存器,Interrupt Descriptor Table(IDT)使用了这些不可见的寄
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
有没有办法将 Dragon NaturallySpeaking 合并到事件驱动程序中?如果我使用 DNS 记录用户语音输入而不将其写入屏幕并直接保存为 XML,我的老板会非常喜欢它。我已经研究了好几天
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 关闭 8 年前。 Improve
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
这个问题在这里已经有了答案: 关闭12年前。 Possible Duplicate: Should I learn C before learning C++? 作为一名专业 (Java) 程序员和重
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 6年前关闭。 Improve thi
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在将一个使用串行端口的 C++ 程序(更准确地说是 Borland“组件”)“转换”为 Java。该程序使用一个线程来监听串行端口,并在收到给定的“事件字符”之一(可能是回车、ACK、NACK .
我是克里斯。我对编码很陌生,我不知道很多术语,这是我第三次也是最成功的学习 Java 的尝试。是的,如果我还年轻,效果就不太好。无论如何,我正在尝试创建游戏,但遇到了一个小问题。 我制作了一个 16
我是一名优秀的程序员,十分优秀!