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