Sign in to follow this  

Functors and Function pointers

This topic is 4555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?
}
}

Share this post


Link to post
Share on other sites
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 dereference

double (MathsClass::*member_fp)(double) = &MathsClass::sqrt; // Need the &

(MathsClass.*member_fp)(20.0); // Have to call it like this

Share this post


Link to post
Share on other sites

This topic is 4555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this