Re: Don't understand odd looking typedef
Angus wrote:
typedef CInterface::IFStatus (CInterface::*Action)();
Can someone explain what is going on here please.
Function pointer syntax is inherited from C, and it's a bit
complicated, although once you get the hang of it, it's not that hard.
Let's assume we have a function which takes an int as parameter and
returns a boolean. The type of a pointer to that function would then be
bool (*)(int)
The slight twist here is how you create an actual pointer variable of
that type. It is created by putting the name of the variable after the *
symbol, for example:
bool (*funcPtr)(int) = 0;
That creates a variable named 'funcPtr' and initializes it with 0.
Method pointers add another element to this definition. Let's assume
that the function is actually a member function of a class named A. In
that case the above line would become:
bool (A::*funcPtr)(int) = 0;
Now 'funcPtr' is a pointer to a member function of A. (As a side note,
this member function can even be virtual. It will still work ok.)
What if we wanted to create a typedef alias instead of an actual
variable? There's an easy rule of thumb for that: Do it exactly as if
you were creating a variable, but just add "typedef" at the beginning.
In other words, if we wanted to create a typedef alias named FuncPtr of
the above type, it would be:
typedef bool (A::*FuncPtr)(int);
Now we can create an actual pointer variable using that alias:
FuncPtr funcPtr = 0;
In your case:
typedef CInterface::IFStatus (CInterface::*Action)();
this defines a typedef alias named Action, which is a pointer to a
member function of the class CInterface. This function takes no
parameters and returns a value of type CInterface::IFStatus.