Don't understand odd looking typedef

Discussion in 'C++' started by Angus, Mar 31, 2008.

  1. Angus

    Angus Guest

    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.
     
    Angus, Mar 31, 2008
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Mar 31, 2008
    #2
    1. Advertising

  3. 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.
     
    Juha Nieminen, Mar 31, 2008
    #3
  4. Angus

    Andre Kostur Guest

    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.
     
    Andre Kostur, Mar 31, 2008
    #4
  5. 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
     
    Andrey Tarasevich, Mar 31, 2008
    #5
  6. Angus

    matish Guest

    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
     
    matish, Mar 31, 2008
    #6
  7. 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
     
    Andrey Tarasevich, Mar 31, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Speer

    Odd behavior with odd code

    Michael Speer, Feb 16, 2007, in forum: C Programming
    Replies:
    33
    Views:
    1,162
    Richard Heathfield
    Feb 18, 2007
  2. Sanchit

    unable to understand this typedef

    Sanchit, Mar 20, 2008, in forum: C Programming
    Replies:
    1
    Views:
    274
    Eric Sosman
    Mar 20, 2008
  3. oor
    Replies:
    0
    Views:
    1,416
  4. André Hänsel

    I don't understand typedef example

    André Hänsel, Mar 18, 2009, in forum: C Programming
    Replies:
    27
    Views:
    888
    Keith Thompson
    Mar 30, 2009
  5. MaggotChild
    Replies:
    1
    Views:
    105
    MaggotChild
    Jun 27, 2010
Loading...

Share This Page