C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Pointer to function Typedefs

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Fri Feb 10, 2006 2:06 pm    Post subject: Pointer to function Typedefs Reply with 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

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





PostPosted: Fri Feb 10, 2006 3:06 pm    Post subject: Re: Pointer to function Typedefs Reply with quote



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





PostPosted: Fri Feb 17, 2006 4:06 pm    Post subject: Re: Pointer to function Typedefs Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.