Jump to content
  • Advertisement
Sign in to follow this  
Wavesonics

C++ functor casting

This topic is 3940 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

I'm currently porting the .lwo loader that is supplied with the LightWave SDK to the Wii. For certain reasons we need it to compile under C++ rather then C (it is written in C). The porting was going fine, pretty much everything done, then we ran into a small problem. There is a function in the library with the prototype: void lwListFree( void *list, void ( *freeNode )( void * )); And a a call to that function with the following function as the second argument: void lwFreePlugin( lwPlugin *p ); Clearly, the argument specified for the supplied function is a lwPlugin pointer rather then a void pointer which is what is expected by the first function. In C this compiled just fine implicitly casting lwPlugin* to void*. But in C++ it simply won't compile. (On our compiler anyway, which we are locked into.) We've tried compiling just the list file as a C file and extern "C" linking it, but still not good. So my question is, can I cast the arguments of a functor when I pass it in?

Share this post


Link to post
Share on other sites
Advertisement
Well, first off, this is a function pointer, not a functor. In C++, the term functor is usually used to indicate a function object.

In any case, in this situation you can probably just use a cast. Ex:

typedef void (*FreeNodeFn)(void *);

lwListFree(list, reinterpret_cast<FreeNodeFn>(lwFreePlugIn));

Share this post


Link to post
Share on other sites
How about explicitly casting the function pointer?
typedef void (*VOID_FUNC)(void *);
lwListFree(list, reinterpret_cast<VOID_FUNC>(lwFreePlugin));


Edit: Curses! Foiled by that villain, SiCrane!

Share this post


Link to post
Share on other sites
Ah ok I thought Functor was just short for function pointer, thanks for clearing that up.

I took something I was reading along with what you said and come up with this:
reinterpret_cast<void (*)(void *)>( lwFreePlugin )

And that compiles, still working on some other things to find out if it all still works, but thanks for the help man!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!