14

Firstly, I've got functions like this.

void func1();
void func2();
void func3();

Then I create my typedef for the array:

void (*FP)();

If I write a normal array of function pointers, it should be something like this:

FP array[3] = {&func1, &func2, &func3};

I want to make it a constant array, using const before "FP", but I've got this error messages:

error: cannot convert 'void ( * )()' to 'void ( * const)()' inialization

PD: Sorry my bad English.

EDIT:

x.h

typedef void (*FP)();

class x
{
 private:
  int number;
  void func1();
  void func2();
  void func3();
  static const FP array[3];
}

x.cpp

const FP x::array[3] = {&x::func1, &x::func2, &x::func3};

My code is more large and complex, this is a summary

Facon
  • 679
  • 2
  • 7
  • 21

5 Answers5

8

Then I create my typedef for the array: void (*FP)();

Did you miss typedef before void?

Following works on my compiler.

 void func1(){}
 void func2(){}
 void func3(){}

 typedef void (*FP)();


 int main()
 {
     const FP ar[3]= {&func1, &func2, &func3};
 }

EDIT

(after seeing your edits)

x.h

 class x;
 typedef void (x::*FP)(); // you made a mistake here

 class x
 {
   public:
      void func1();
      void func2();
      void func3();
      static const FP array[3];
 };
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • I was using a class..., it takes me the same error using const FP array[3] = ... or using FP const array[3] = ... – Facon Feb 04 '10 at 15:24
  • 2
    Uhh, you got an good eye, great! Given that his error message said `void (*)()`, i was thinking myself of static member functions, not checking whether they really are. Now all the question is - did he miss writing `static`, or did he typo'ed the typedef or/and did he typo'ed the error message? xD – Johannes Schaub - litb Feb 04 '10 at 16:53
  • @Johannes: Yeah, his question before the edits was a bit confusing(because the code had no errors). xD :-) – Prasoon Saurav Feb 04 '10 at 17:11
  • 1
    Now I've got this error: x.cpp:line: error: must use .* or ->* to call pointer-to-member function in 'x::array[((int)((x*)this)->x::number)] (...)' Calling the array in another x class method. How do you call it? – Facon Feb 04 '10 at 21:22
  • @PrasoonSaurav Highlighting the method to access a function from the array would complete the post. – madD7 Feb 02 '16 at 06:58
7

without typedef:

void (*const fp[])() = {
    f1,
    f2,
    f3,
};
vlk
  • 2,581
  • 3
  • 31
  • 35
2
    typedef void (*FPTR)();

FPTR const fa[] = { f1, f2};
// fa[1] = f2;  You get compilation error when uncomment this line.
Jagannath
  • 3,995
  • 26
  • 30
2

Which compiler are you using? This works on VS2005.

#include <iostream>

void func1() {std::cout << "func1" << std::endl;}
void func2() {std::cout << "func2" << std::endl;}
void func3() {std::cout << "func3" << std::endl;}

int main()
{
int ret = 0;

typedef void (*FP)();

const FP array[3] = {&func1, &func2, &func3};

return ret;
}
sand
  • 542
  • 6
  • 16
1

If you want the array itself to be const:

FP const a[] =
    {
        func1,
        func2,
        func3
    };
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Whether he writes `FP const` or `const FP` makes no difference. See this question: http://stackoverflow.com/questions/1808471/is-const-lpvoid-equivalent-to-void-const – Johannes Schaub - litb Feb 04 '10 at 15:41
  • @ltib: While I agree, I'm not sure I see the relevance of your comment. Perhaps you're just observing, in which case, cheers. – John Dibling Feb 04 '10 at 18:28
  • I was just a bit confused, since your answer and the question are basically identical. Just that your's is a non-member array (and that you have the const at another place). So i'm not seeing the relevance of your answer. But maybe you are just observing likewise, so cheers. – Johannes Schaub - litb Feb 07 '10 at 00:24