c++ - Typedef function pointer? -
i'm learning how dynamically load dll's don't understand line
typedef void (*functionfunc)(); i have few questions. if able answer them grateful.
- why
typedefused? - the syntax looks odd; after
voidshould there not function name or something? looks anonymous function. - is function pointer created store memory address of function?
so i'm confused @ moment; can clarify things me?
typedef language construct associates name type.
use same way use original type, instance
typedef int myinteger; typedef char *mystring; typedef void (*myfunc)(); using them like
myinteger i; // equivalent int i; mystring s; // same char *s; myfunc f; // compile equally void (*f)(); as can see, replace typedefed name definition given above.
the difficulty lies in pointer functions syntax , readability in c , c++, , typedef can improve readability of such declarations. however, syntax appropriate, since functions - unlike other simpler types - may have return value , parameters, lengthy , complex declaration of pointer function.
the readability may start tricky pointers functions arrays, , other more indirect flavors.
to answer 3 questions
why typedef used? ease reading of code - pointers functions, or structure names.
the syntax looks odd (in pointer function declaration) syntax not obvious read, @ least when beginning. using
typedefdeclaration instead eases readingis function pointer created store memory address of function? yes, function pointer stores address of function. has nothing
typedefconstruct ease writing/reading of program ; compiler expands typedef definition before compiling actual code.
example:
typedef int (*t_somefunc)(int,int); int product(int u, int v) { return u*v; } t_somefunc afunc = &product; ... int x2 = (*afunc)(123, 456); // call product() calculate 123*456
Comments
Post a Comment