【发布时间】:2020-09-27 15:34:40
【问题描述】:
我想为我的班级写一个包装函数...我真的不知道该怎么做!
看,我想要一个,比如说,run(),函数,接受一个函数作为参数,这很容易。
用法类似于
void f() { }
void run(int (*func)) {
//whatever code
func();
//whatever code
}
run(f);
应该只运行f() 函数,对吧?
但是如果f() 有必需的参数呢?假设它被声明为f(int i, int j),我将继续重写run()函数以分别接受那些ints,并将它们传递给f()函数。
但我希望能够将 Any 函数传递给 run(),无论参数有多少,或者它们是什么类型。意思是,最后,我希望得到类似于我所期望的假设的用法
void f() {int i, int j}
void v() {char* a, int size, int position}
void run(int (*func)) {
//whatever code
func();
//whatever code
}
run(f(1, 2));
run(v(array, 1, 2));
要做。我知道这看起来很愚蠢,但我认为我的意思是正确的。
我该怎么做?
请记住这是 arduino-c++,所以它可能缺少一些东西,但我相信有一些库可以弥补这一点......
【问题讨论】:
标签: c++ pointers arduino arguments arduino-c++