0

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

3
  • 2
    Can't reproduce, please post an mvce. Commented Aug 12, 2015 at 7:51
  • A is different from streamHandler, isn't it? Commented Aug 12, 2015 at 7:59
  • This still isn't an mvce. Post code without syntax errors which displays the issue you have encountered. Commented Aug 12, 2015 at 8:08

2 Answers 2

4

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your answer. But it gives me unresolved external symbol for A::Function1 and A::Function2
@WerBn make sure you link all of your object files together.
@WerBn you need to link the object file where you define those functions,
Ya :P Good point, I forgot to define the functions. Thank you very much. I almost got it. Real problem was the definition of the array in the .h file. Thank you!
Btw, do you know a way of initializing dinamically this array? Just imagine it's not a static array pointing for non-static member functions. Can you also list-initialize it? I«m trying, but I can't
|
3

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.

10 Comments

Using std::function here is incredibly overkill.
it is more readable and it will allow him to easily use stl algorithms on his function pointer containers. Unless you're in an environment where nanoseconds matter, I wouldn't go down the function pointer route. I will never understand why people are so reluctant to new modern ways of coding.
using f_type = void (*)(void) can't really be said to hurt readability in my opinion, and std algorithms work perfectly well with function pointers. std::function is not a "modern function pointer" (whatever that coud mean), it's a type-erased functionoid container.
Why std::vector ? Is is static this way? I would like to stick to old faction and use function pointer without a template. But thank you anyway :)
feel free to change it to std::array.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.