 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri Feb 10, 2006 2:06 pm Post subject: Pointer to function Typedefs |
|
|
I have seen lot of definition like the one below in code
typedef void (*func_cback) (arg1, arg2)
I need to know what this means in layman terms
Thanks in Advance
KIRAN
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Fri Feb 10, 2006 3:06 pm Post subject: Re: Pointer to function Typedefs |
|
|
kirantsbe (AT) gmail (DOT) com wrote:
| Quote: | I have seen lot of definition like the one below in code
typedef void (*func_cback) (arg1, arg2)
I need to know what this means in layman terms
|
Firstly, you simply read this from the inside out and from left to right.
- func_cback
func_cback is
- (*func_cback)
func_cback is a pointer
- (*func_cback)(arg1, arg2)
func_cback is a pointer to a function taking an arg1 and an arg2
- void (*func_cback)(arg1, arg2)
func_cback is a pointer to a function taking an arg1 and an arg2 and
returning nothing
The typedef in front now makes this not the definition of a function pointer
but a typedef for a function pointer.
Secondly, the IMHO easier syntax is to use a function typedef instead of a
function pointer typedef:
#if 0
typedef void (*func_cback) (arg1, arg2);
funct_cback cb = &some_function;
#else
typedef void func_cback(arg1, arg2);
func_cback* cb = &some_function;
#endif
This way makes it more obvious that 'cb' is a pointer here.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Seungbeom Kim Guest
|
Posted: Fri Feb 17, 2006 4:06 pm Post subject: Re: Pointer to function Typedefs |
|
|
Ulrich Eckhardt wrote:
| Quote: | Secondly, the IMHO easier syntax is to use a function typedef
instead of a
function pointer typedef:
#if 0
typedef void (*func_cback) (arg1, arg2);
funct_cback cb = &some_function;
#else
typedef void func_cback(arg1, arg2);
func_cback* cb = &some_function;
#endif
This way makes it more obvious that 'cb' is a pointer here.
|
Unfortunately, this doesn't work with pointer to member types:
struct S { void f(); };
// OK
typedef void (S::*pft)();
pft pf = &S::f;
// Error
typedef void (S::ft)();
ft* pf = &S::f;
I used to like the idea of using a function typedef instead of a
function pointer typedef myself, but now with pointers to members,
that spoils the consistency and I cannot make up my mind. :(
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|