Don't understand odd looking typedef

A

Angus

I have this typedef:

typedef CInterface::IFStatus (CInterface::*Action)();


It is used in a state machine function to call a pertinent function

It is used like this:

static const Action actiontable [] =

Which is an array of the functions to call (in particular states.

I am having trouble working out what the typedef is doing? Eg to use
without the typedef what would I substitute for Action?

Can someone explain what is going on here please.
 
V

Victor Bazarov

Angus said:
I have this typedef:

typedef CInterface::IFStatus (CInterface::*Action)();

'Action' is a pointer to member of CInterface, a non-static function
that takes no arguments and returns 'CInterface::IFStatus'.
It is used in a state machine function to call a pertinent function

It is used like this:

static const Action actiontable [] =

Which is an array of the functions to call (in particular states.

I am having trouble working out what the typedef is doing? Eg to use
without the typedef what would I substitute for Action?

Any other name. Without the typedef you'd be declaring a variable
of the same type (a pointer to member of ... ).
Can someone explain what is going on here please.

I hope I have. Ask more questions if something is unclear.

V
 
J

Juha Nieminen

Angus said:
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.
 
A

Andre Kostur

I have this typedef:

typedef CInterface::IFStatus (CInterface::*Action)();


It is used in a state machine function to call a pertinent function

It is used like this:

static const Action actiontable [] =

Which is an array of the functions to call (in particular states.

I am having trouble working out what the typedef is doing? Eg to use
without the typedef what would I substitute for Action?

Can someone explain what is going on here please.


This is a typedef to a pointer-to-member-function of the class CInterface
which returns a CInterface::IFStatus object by value, and takes no
parameters.
 
A

Andrey Tarasevich

Angus said:
I have this typedef:

typedef CInterface::IFStatus (CInterface::*Action)();


It is used in a state machine function to call a pertinent function

It is used like this:

static const Action actiontable [] =

Which is an array of the functions to call (in particular states.

I am having trouble working out what the typedef is doing? Eg to use
without the typedef what would I substitute for Action?

(In addition to other responses)

Without 'typedef' you'd have to declare your 'actiontable' as

static CInterface::IFStatus (CInterface::* const actiontable[])() =
 
M

matish

Victor Bazarov ha scritto:
typedef CInterface::IFStatus (CInterface::*Action)();
'Action' is a pointer to member of CInterface, a non-static function
that takes no arguments and returns 'CInterface::IFStatus'.

How would it be if the function were static?

typedef CInterface::IFStatus (*CInterface::Action)();

?

ty
 
A

Andrey Tarasevich

matish said:
Victor Bazarov ha scritto:
typedef CInterface::IFStatus (CInterface::*Action)();

How would it be if the function were static?

typedef CInterface::IFStatus (*CInterface::Action)();
...

When it comes to function type, static functions are ordinary non-member
functions. So the syntax would be exactly the same as with a regular
non-member function

typedef CInterface::IFStatus (*Action)();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top