Functors and Function pointers

Started by
6 comments, last by Oralloy 18 years, 9 months ago
where can i find comprheensive tutorial about functors and function pointers
Essegin Ziki:) bunu bu sitede imza hesabına yazsam kimse anlamaz!!!:)
Advertisement
I've always liked this site.
Switch_With_Function_Pointer(2, 5, &Minus);

Should that addressof relly be there? I always though you just pass the name of the function..
Quote:Original post by DvDmanDT
Switch_With_Function_Pointer(2, 5, &Minus);

Should that addressof relly be there? I always though you just pass the name of the function..


It's ok to have it there and you always need it when taking the address of a member function but basicly it's just a stylistic taste choice.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
That's not entirely correct. According to the standard it should be there. Only the Microsoft compiler also allows you to leave it out (both in static functions and in member-functions), but GCC for example will require you to have the & there.
Quote:Original post by rick_appleton
That's not entirely correct. According to the standard it should be there. Only the Microsoft compiler also allows you to leave it out (both in static functions and in member-functions), but GCC for example will require you to have the & there.


Well, thats nice to know! <OT>Do you have to have the dereferencing in other compilers too?
vod (*Render)();InfLoop(){    if(Render)  // check to see if its a valid pointer    {       Render(); // Can I still do this       (*Render)(); // Or do I have to do this for non-M$ compilers?    }}

Hmm, not sure on the usage. I believe I'm always doing the (*blahfunc)() thing, but I'm not positive.
Quote:
Well, thats nice to know! <OT>Do you have to have the dereferencing in other compilers too?


With GNU gcc you can always call a regular function pointer like a regular function (case one of your example). I'm 99% sure than this is in the standard. Member functions on the other hand have to be dereferenced with .* or ->* when called. Also, when taking the address of a regular function, it is optional whether to use the address operator &, but when taking the address of a member function then the address of operator is compulsory (although gcc will compile with a warning if it is left out).

double (* function_pointer)(double) = sqrt; // No & necessary (but is optional)function_pointer(20.0); // Don't need to dereferencedouble (MathsClass::*member_fp)(double) = &MathsClass::sqrt; // Need the &(MathsClass.*member_fp)(20.0); // Have to call it like this

This topic is closed to new replies.

Advertisement