Re: strang function pointer from faq book (help)

From: Karl Heinz Buchegger (kbuchegg_at_gascad.at)
Date: 03/29/05


Date: Tue, 29 Mar 2005 17:25:55 +0200

tomailmelookatbottom@rediffmail.com wrote:
>
>
> I am having doubt with the line
>
> typedef void(Fred::*FredMemberPt)(int);
>
> In the above line I can able to understand that it is a declaration of
> pointer to a function which is typedef ed. I have seen using the scope
> resolution operator used to access the static variouble,member function
> definition outside the class(and lot more,yet to learn). But the above
> function name FredMemberPt does not exist in any of the code and yet it
> has been compiled with out any errors.

It is just an ordinary typedef.
Generally the rule works like this: Just write it as an ordinary
declaration eg.

   int MyJ;

Then preceed the whole thing with 'typedef' and the name you define,
'MyJ', magically turns into a new data type name which is an alias
for the whole. So if you do

  typedef int MyJ;

then MyJ gets an alias for the whole data type, in this case for 'int'.

In light of that

   typedef void(Fred::*FredMemberPt)(int);

says:
  FredMemberPt is an alias name for a data type. This data type is a
  pointer to a member function of Fred, and it takes an int as argument
  and returns void.

It is used to get a more friendly writing a little bit down. To define
an actual function pointer one would need to write:

int main()
{
  void (Fred::*gFnct)(int) = &Fred::g;
}

which defines gFnct to be a pointer to a member function of Fred. Instead of
that clumsy syntax, one introduces a typedef to make that whole thing
cleaner:

int main()
{
  FredMemberPt gFnct = &Fred::g;
}

-- 
Karl Heinz Buchegger
kbuchegg@gascad.at


Relevant Pages

  • Re: RE:How to realize the OOP in C?
    ... > int n; ... be a pointer to a standard structure like: ... typedef struct object_of_fubar * ObjectOfFubarP; ... Note that the class structure is allocated only ...
    (comp.lang.c)
  • Re: I dont understand typedef example
    ... * Using typedef, declare 'func' to have type ... 'function taking two int arguments, ... declared ptr as a pointer object that can points to a function of the ...
    (comp.lang.c)
  • Re: typedef with function pointers
    ... typedef has the same syntax as defining variables. ... The above defines an array called "arr" with five int elements. ... Each element is a pointer ... typedef char frpapfrc; ...
    (comp.lang.c)
  • Re: Ran across this cryptic code for C, I got blown away
    ... > programming in C for 3 months now). ... It says that `f_p' is a synonym for the type: "a pointer to a function ... that takes an `int' as an argument and returns an `int'. ... > I have a feeling addn returns a pointer that was defined in typedef. ...
    (comp.lang.c)
  • Data Type Ranges
    ... various data type, in byte, like int and pointer (examples: CMyClass* ...
    (microsoft.public.windowsce.embedded.vc)