简体   繁体   English

C ++静态函数指针数组的初始化

[英]C++ Initialization of static function pointer array

I want to create a static function pointer array, so I can jump to a certain function regarding a received index. 我想创建一个静态函数指针数组,因此可以跳转到有关接收到的索引的某个函数。 Like an index jumper. 就像索引跳线一样。

So imagine a class like this: 因此,想象一个像这样的类:

Class A 
{ 
  private:
  static void 1stFunction();
  static void 2ndFunction();

  static void(*functionPointer[20])(void);

};

Then I would like that functionPointer to get the value of the 1stFunction and 2ndFunction, and maybe even more. 然后,我希望那个functionPointer获得1stFunction和2ndFunction的值,甚至更多。 So, how do I initialize it? 那么,如何初始化它? As far as I know, when a static member is declared, you can use it even before an instance is created. 据我所知,声明静态成员后,甚至可以在创建实例之前使用它。 So I though, lets initialize that function pointer, so later I can call it like this 因此,尽管如此,让我们初始化该函数指针,以便以后我可以像这样调用它

functionPointer[receivedIndex]();

So i tried to initilize it like this, in the same .h file 所以我试图在相同的.h文件中像这样初始化它

void (*A::functionPointer[])(void) =
{
    A::1stFunction,
    A::2ndFunction,
};

But the compiler gives me redifinition, it says it's already created. 但是编译器给了我重新定义,说它已经创建了。

So, pretty sure I'm missing something. 所以,很确定我缺少什么。 I don't know though, if it is syntax or simply it is not possible to do it this way. 我不知道,如果是语法还是根本就不可能这样。 I know that function pointers to class's member functions are different than normal function pointers... But this is a static function, so I believe it doesn't belong to an instance and therefore it should work with normal function pointers. 我知道指向类成员函数的函数指针与普通函数指针不同……但这是一个静态函数,因此我认为它不属于实例,因此应该与普通函数指针一起使用。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

The following would be a working example that probably achieves what you need. 以下将是一个可能会满足您需要的工作示例。

You need C++11 for the initializer list. 您需要C++11作为初始化列表。

It is a good practice to initialize the static member in the cpp file, as you don't want to have a definition of the static member everytime the header is included (this can lead to linking issues). 最好在cpp文件中初始化静态成员,因为您不希望在每次包含标头时都对静态成员进行定义(这可能会导致链接问题)。

You can call callf with the desired index and have the corresponding function called, based on the initialization of the function pointer array. 您可以根据函数指针数组的初始化,使用所需的index调用callf并调用相应的函数。

The output of the program would be: 该程序的输出为:

I am 2ndFunction

Header file 头文件

class A
{
private:
  static void Function1();
  static void Function2();

  static void(*functionPointer[20])();

public:
  static void callf(int index);
};

Implementation 实作

#include <iostream>
#include "ex.h"

void(*A::functionPointer[20])() {
  A::Function1,
  A::Function2
};

void A::Function1() {
  std::cout << "I am 1stFunction" << std::endl;
}

void A::Function2() {
  std::cout << "I am 2ndFunction" << std::endl;
}

void A::callf(int index) {
  A::functionPointer[index]();
}

int main(int argc, char const *argv[]) {
  A::callf(1);
  return 0;
}

Here you have a more modern C++ approach (C++14 needed) I would advise you to explore lambda functions if you are not restricted to C++03. 在这里,您有一个更现代的C ++方法(需要C ++ 14),如果您不限于C ++ 03,我建议您探索lambda函数。

#include <iostream>
#include <functional>
#include <vector>

class A {
public:

  using f_type = std::function<void(void)>;
  f_type f1 = []() { std::cout << "f0" << std::endl;};
  f_type f2 = []() { std::cout << "f1" << std::endl;};
  static void f3() { std::cout << "f3" << std::endl; }

  std::vector<f_type> functions{f1, f2, f3};

};


int main() {

  A a;
  a.functions[0]();
  a.functions[1]();
  //adding custom lambda
  a.functions.emplace_back([](){ std::cout << "custom f" << std::endl;});
  a.functions[2]();

  return 0;
}

you can add both functions and lambdas to your container. 您可以将函数和lambda都添加到容器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM