简体   繁体   中英

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. 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

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.

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).

You can call callf with the desired index and have the corresponding function called, based on the initialization of the function pointer array.

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.

#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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