Initializing an array of pointers to functions

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
I am writing a dialog based MFC appliation using Visual C++ 6.0. In my simplified test case, I added the following definitions to the header (TestDlg.h).
 typedef void FOO();
 FOO show1{};
 FOO show2{};
 FOO show3{};
 FOO *pfun[3];
At the end of the OnInitDialog() code (before return TRUE;), I attempted to initialize the array as follows:
 pfun[0] = show1;
 pfun[1] = show2;
 pfun[2] = show3;
Each of these lines has compile error C2440: = : cannot convert from void (__cdecl CTestDlg::*)(void) to void (__cdecl *)(void)
What is the proper way to initialize a function pointer array?
------------------------------------ Two other things----------------
1. It would be much nicer to initialize the array in the header file, but the MFC app compiler apparently does not allow array initilizations in the header, like
 int test[] = { 1, 2, 3 };      // MFC compiler does not allow this in the header file
 FOO *pfun[3] = { show1, show2, show3 };  // MFC does not allow this in header
Could you enlighten me to why this is not permitted?
2. I tried a couple other variations to defining the function pointer array in the header, and both received compile errors:
 void (*pfun[3]());    // Compile error explanation says an array of functions is
                                 //  not allowed, but an array of pointers to functions is!
 typedef void *FOO();
 FOO pfun[3];        // Same compiler error
Both the previous methods for defineing a function pointer array work for console applications, but the MFC compiler objects when these definitions are in the header file.  I suspect this is a bug ??
Thanks.

View the full article
 
Back
Top