Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Don't understand odd looking typedef

Reply
Thread Tools

Don't understand odd looking typedef

 
 
Angus
Guest
Posts: n/a
 
      03-31-2008
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-31-2008
Angus wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      03-31-2008
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.
 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      03-31-2008
Angus <> wrote in news:6c64a91a-264a-43fb-b454-
:

> 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.

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-31-2008
Angus wrote:
> 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[])() =

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
matish
Guest
Posts: n/a
 
      03-31-2008
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
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      03-31-2008
matish wrote:
> 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)();
> ...


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)();

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I don't understand typedef example André Hänsel C Programming 27 03-30-2009 03:40 PM
CRTP-problem: How can the base class typedef a derived class' typedef? oor C++ 0 05-20-2008 12:39 PM
unable to understand this typedef Sanchit C Programming 1 03-20-2008 02:38 PM
Odd behavior with odd code Michael Speer C Programming 33 02-18-2007 07:31 AM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57