# C++ functor casting

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

## 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 on other sites
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 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 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 on other sites
The code works beautifully on the Wii now, thanks for the help guys!

1. 1
Rutin
35
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 98
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009651
• ### Who's Online (See full list)

There are no registered users currently online

×