Hello. I'm trying to create a class containing a static data member which is an array of function pointers, but I can't work out the syntax to define and initialise it in the corresponding source file. I don't have a problem with defining a global array of function pointers however.

Static variables exist before any objects are created. So, a static pointer can't point to a function that only exists as part of an object.

11-01-2005

Yarbles

Hello 7stud.

Whilst it is true that (class) static variables are created prior to main() being called, surely this is also the case for globals too, which in the example above compiles OK. Also, member functions do not exist as part of an object in the same way that data members do; they are created at an individual area in memory during compilation as per non-member functions (I think). The member function address is bound to an object whenever that object calls the function, but you do not have to create any objects for the member functions to exist in memory. Therefore there is no problem, as far as I can tell, in assigning a member function address to a class static function pointer; it is the syntax for this operation that I can't work out.

11-02-2005

7stud

Quote:

The member function address is bound to an object whenever that object calls the function, but you do not have to create any objects for the member functions to exist in memory.

What would be the difference between static member functions and regular member functions?

11-02-2005

Yarbles

As far as function pointers are concerned, non-static member functions are accessed via the pointer-to-member operators, whereas a static member function is accessed via a regular pointer. What I was getting at is that if you define a class and define member functions for that class, it is legitimate to initialise a pointer to a member function (non-static or static) despite having created no objects, as below:

Hold everything. I made a mistake when I was testing your code: I used the pointer to execute the static function, which of course should work without having to create any objects. I don't get any compile errors with your code, but if I add a line in main() to execute the non-static function:

pf1(); //error C2064: term does not evaluate to a function

I need to create an object before I can execute the function:

Feeble aFeeble;
(aFeeble.*pf1)();

I guess that's what you've been saying all along: you can assign pointers to the non-static member functions before objects exist, but you can't execute the function unless you use an object.