【问题标题】:C++: array of pointers to functionsC++:指向函数的指针数组
【发布时间】:2021-12-07 23:01:31
【问题描述】:

假设我们有 2 个函数

foo() { cout << "Hello"; }
foo2() { cout << " wolrd!"; }

如何创建一个指针数组(比如ab),其中a 指向foo()b 指向foo2()? 我的目标是将这些指针存储在数组 A 中,然后遍历 A 以执行这些函数。

【问题讨论】:

  • 因为您使用的是 C++ 而不是 C,所以使用 std::function,而不是原始指针。
  • @Jepessen 你不需要std::function,只要需要。普通函数指针类型也适用于给定的情况。
  • @Jepessen:我们应该避免使用原始的 OWNING 指针。常规指针可能没问题。
  • 您忘记了函数的返回类型,无论您遇到什么问题,都可能是因为编译器处于 GNU(或其他不兼容)模式,默认类型为 int
  • @Jarod42 my goal is to store these pointers in an array A, then loop over A to execute these functions. 在我看来,std::function 是实现其目标的更好、更清洁、更安全的解决方案。那么显然可以使用原始指针。

标签: c++ pointers


【解决方案1】:

您可以按如下方式使用类型化函数指针:

using FunPtrType = void(*)();

FunPtrType arr[]{&foo,  &foo2};
// or
std::array<FunPtrType, 2> arr2{&foo,  &foo2};

// ... do something with the array of free function pointers
// example
for(auto fun: arr2)
    fun();

【讨论】:

  • 我建议不要为指针使用类型别名。由于隐藏了星号,它们使程序不那么明显。
  • 我在 std::array arr2{&foo, &foo2};有什么想法吗?
  • #include &lt;array&gt;了吗?从 C++17 开始不需要指定模板参数:godbolt.org/z/jMME54r75 啊,你忘了指定函数的返回类型foo() ==> void foo()
  • @eerorika (IMO) 的使用使得函数指针类型比some retunr type(*)( a lot of arguments) 更具可读性 + 在整个代码库中易于编辑。
  • @eerorika 风格问题。有些人可能会争辩说void (*arr[])() {&amp;foo, &amp;foo2}; 的可读性较差。它看起来更像是某个遭遇交通事故的 lambda
【解决方案2】:

有一个简单的实现:

#include <iostream>
#include <vector>
using namespace std;
// Defining test functions
void a(){cout<<"Function A"<<endl;}
void b(){cout<<"Function B"<<endl;}

int main()
{
    /*Declaring a vector of functions 
      Which return void and takes no arguments.
    */
    vector<void(*)()> fonc;
    //Adding my functions in my vector
    fonc.push_back(a);
    fonc.push_back(b);
    //Calling with a loop.
    for(int i=0; i<2; i++){
        fonc[i]();
    }
    return 0;
}


【讨论】:

    【解决方案3】:

    现在不需要 typedef,只需使用 auto

    #include <iostream>
    void foo1() { std::cout << "Hello"; }
    void foo2() { std::cout << " world!"; }
    auto foos = { &foo1, &foo2 };
    int main() { for (auto foo : foos) foo(); }
    

    【讨论】:

      【解决方案4】:

      两种等效的方法可以做你想做的事:

      方法一

      
      #include <iostream>
      void foo() 
      { 
          std::cout << "Hello";
      }
      void foo2() 
      { 
          std::cout << " wolrd!"; 
          
      }
      
      
      int main()
      {
         
          void (*a)() = foo;// a is a pointer to a function that takes no parameter and also does not return anything
          
          void (*b)() = foo2;// b is a pointer to a function that takes no parameter and also does not return anything
          
          
          //create array(of size 2) that can hold pointers to functions that does not return anything and also does not take any parameter
          void (*arr[2])() = { a, b};
          
          arr[0](); // calls foo 
          
          arr[1](); //calls foo1
          
          return 0;
      }
      
      

      方法一可以执行here

      方法二

      
      #include <iostream>
      void foo() 
      { 
          std::cout << "Hello";
      }
      void foo2() 
      { 
          std::cout << " wolrd!"; 
          
      }
      
      
      int main()
      {
         
          //create array(of size 2) that can hold pointers to functions that does not return anything
          void (*arr[2])() = { foo, foo2};
          
          arr[0](); // calls foo 
          
          arr[1](); //calls foo1
          
          return 0;
      }
      
      

      方法2可以执行here

      【讨论】:

        猜你喜欢
        • 2014-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-25
        相关资源
        最近更新 更多