typedef functions?

This is a discussion on typedef functions? within the C++ Programming forums, part of the General Programming Boards category; I'm having understanding this concept. When I write, typedef PV(int) {.........} PV hello(int, int){........} I don't see how the function ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    47

    typedef functions?

    I'm having understanding this concept. When I write,

    typedef PV(int) {.........}

    PV hello(int, int){........}

    I don't see how the function could be a type basically. Is it calling that function everytime? Or is hello now another name? If so, I don't see how it could have a different parameter list.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I have never seen typedef used like that and i'm fairly sure that that sort of use is invalid. I'm surprised your compiler did not throw errors at you.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    47
    let me show you the example from the book.

    typedef void (*SIG_TYP)(int);
    typedef void (*SIG_ARG_TYP)(int);
    SIG_TYP signal(int, SIG_ARG_TYP);

    can anyone explain this?

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Assume the following type is defined:

    Code:
    typedef int function_t (int);
    Then this can be used as:

    Code:
    function_t *myfunction;
    In this example the function myfunction is of type pointer to function which returns an int and has a parameter of type int.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    yes they are function pointers.

    typedef void (*SIG_TYP)(int); == pointer to func that takes an int and returns void. aliased as SIG_TYP

    typedef void (*SIG_ARG_TYP)(int); == as above but aliased as SIG_ARG_TYP

    SIG_TYP signal(int, SIG_ARG_TYP);

    function called signal that takes an int and a SIG_ARG_TYP function pointer and returns a pointer to a function of type SIG_TYP. Both of these are only aliases for a pointer to a function that takes an int parameter and returns nothing.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >typedef void (*SIG_TYP)(int);
    >typedef void (*SIG_ARG_TYP)(int);

    When defining:

    SIG_TYP myfunction;
    SIG_ARG_TYP myfunction;

    then myfunction is a pointer to a function returning void and which has a parameter of type int.

    >SIG_TYP signal(int, SIG_ARG_TYP);

    Here signal is a pointer to a function of type void which requires an int and a function of type SIG_ARG_TYP.

    Note: The typedef of SIG_TYP and the implementation of signal is different. SIG_TYP requires an int as parameter, while signal has a int as parameter AND a variable of type SIG_ARG_TYP. Hmm, I don't understand this. Perhaps a copy-mistake?

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    47
    I highly doubt it, although it's possible, it's out of Stroustrup's 2000 edition book. I just don't see the correlation between a function and a type. I mean, a struct is just variables, but a function performs an action.

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    they are not functions. They are pointers to functions.these can be used to call the function.they are just memory addresses. thats all any pointer really is.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >I highly doubt it, although it's possible, it's out of Stroustrup's
    >2000 edition book.

    It is ANSI C, but I'm not sure about C++.

    >I just don't see the correlation between a function and a type. I
    >mean, a struct is just variables, but a function performs an
    >action.

    You can't compare types and functions this way.

    Like a variable can be of some type, also a function can be of some type. A struct is a type, but not a variable, though a variable can be of type struct. And in the same way: SIG_TYP is a type, but not a function, though a function can be of type SIG_TYP.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 12:56 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Replies: 7
    Last Post: 11-17-2008, 12:00 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  5. help with classes and member functions
    By syner in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2002, 08:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21