C++;将函数作为参数传递,以在某一点执行 我一直在研究如何将函数作为参数传递给C++中的另一个函数。

C++;将函数作为参数传递,以在某一点执行 我一直在研究如何将函数作为参数传递给C++中的另一个函数。,c++,function-pointers,C++,Function Pointers,我不确定我是否正确理解了这个术语,所以我还没有在网上找到我想要的。我想我可以把它解释成一个问题 我试图把一个函数作为一个参数传递,这个参数发生在它传递的函数的某个点上 下面是一些伪代码,可以更好地解释我的查询: myNewFunction( passedFunction_1, passedFunction_2 ){ //do some magic in OpenGL execute passedFunction_1

我不确定我是否正确理解了这个术语,所以我还没有在网上找到我想要的。我想我可以把它解释成一个问题

我试图把一个函数作为一个参数传递,这个参数发生在它传递的函数的某个点上

下面是一些伪代码,可以更好地解释我的查询:

    myNewFunction( passedFunction_1, passedFunction_2 ){

            //do some magic in OpenGL
                execute passedFunction_1
            //do some magic in OpenGL
                execute passedFunction_2
            //do some magic in OpenGL
                execute passedFunction_1 (again)
            //do some magic in OpenGL
    }

我希望你能理解。任何帮助都将不胜感激。提前感谢。

定义
myNewFunction
,这样它就可以使用另外两个函数。
passFunction1
passFunction2
都是指向不需要参数且不返回任何内容的函数的指针

void myNewFunction(void (*passFunction1)(),
                   void (*passFunction2())
{
    passFunction1();
    passFunction2();
    passFunction1();
}
定义一个不带参数、执行某些操作且不返回任何内容的函数

void foo()
{
   // Do something useful
}
void bar()
{
   // Do something useful
}
定义另一个不带参数、执行某些操作且不返回任何内容的函数

void foo()
{
   // Do something useful
}
void bar()
{
   // Do something useful
}
使用最后两个函数调用第一个函数

myNewFunction(foo, bar);

最新的标准定义了一个类来实现这一点,这可能是最简单的方法(特别是考虑到C函数指针的语法可能很吓人)

语法类似于:

myNewFunction( std::function<void()> passedFunction_1, std::function<void()> passedFunction_2 ){
        //do some magic in OpenGL
            passedFunction_1();
        //do some magic in OpenGL
            passedFunction_2()
        //do some magic in OpenGL
            passedFunction_1();
        //do some magic in OpenGL
}

因为我们讨论的是C++而不是C,所以应该使用。它更安全,而且您还可以使用它来做更复杂的事情,比如测试存在性、绑定/转换

#include <iostream>
#include <functional>

#using namespace std;

typedef function<void (void)> tVoidFunction;
typedef function<void (int)> tVoidFunctionTakingIntParameter;
typedef function<int (float)> tIntFunctionTakingFloatParameter;

void combinedFunction( tVoidFunction fvoid, tVoidFunctionTakingIntParameter fInt, tIntFunctionTakingFloatParameter fIntFromFloat){
  if (fvoid) {
    fvoid();
  }
  if (fInt && fIntFromFloat) {
    static const float magicValue = 42.f;
    int convertedValue = fIntFromFloat(magicValue);
    fInt(convertedValue);
  }
}

void helloWorld() {
  cout << "Hello World" << endl;
}
int floatToInt(float f) {
  return floor(abs(f));
}
void doSomethingWithAnInt(int i) {
  cout << "doSomethingWithAnInt: " << i << endl;
}
...

void doWeirdThings() {
  combinedFunction(helloWorld, doSomethingWithAnInt, floatToInt);
}

你能澄清一下吗?你只需要用C++来做这个语法的帮助吗?是的,差不多。我已经有我的功能了。我只是想按照上面函数的顺序实现一些东西。感谢您的评论。关于函数的指针,它们可以存储在任何东西中,以便在另一个函数中执行吗?我将更新上面的伪代码。是的,它们可以存储起来以备将来使用。哦。我现在和你在一起。我想我明白了。谢谢你澄清这一点。我现在就去试一试。谢谢你的发帖。我建议对所有函数使用typedefs,以提高代码可读性。如果函数将在编译时解析,那么你可以使用模板而不是函数指针。这很有趣。我也要试一试。谢谢你的发帖。回答得很好。这也是一个很好的例子。能否将数组作为正在传递的函数参数之一传递?例如:
typedef function newFunc
你可以使用任何你喜欢的函数签名。答案很棒。这真是太棒了。谢谢你的发帖。