void* to functor casting???

Started by
17 comments, last by The Forgotten Mindset 18 years, 9 months ago
Try:
typedef int (*Functor)();Functor doIntro;Functor doFrame;Functor doOutro;// ...doFrame = reinterpret_cast<Functor>(value);
Advertisement
Thanks Roboguy, that's sounds like it will work, let me try it.
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
Didn't work. Just as I thought, the typedef was an int.
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
A simple solution to this is:
class Value{public:    LPCTSTR m_lpszString;    int m_iInteger;    bool m_bBoolean;    int (*m_fpIntFunc)();    Value(const char* pString) : m_lpszString(pString){}    Value(int iInteger) : m_iInteger(iInteger){}    Value(bool bBoolean) : m_bBoolean(bBoolean){}    Value(int (*fpIntFunc) : m_fpIntFunc(fpIntFunc){}};int (*DoIntro)();int (*DoFrame)();int (*DoOutro)();std::string m_Title;//etcint SetFlag(int iID, Value Flag){    switch(iID)    {        case FLAG_FRAMEFUNC:        {            DoIntro = Flag.m_fpIntFunc;            break;        }        case FLAG_TITLETEXT:        {            m_Title = Flag.m_lpszString;            break;        }    }    return 0;}
Thanks Programmer16, that looks good. I could probably just copy/paste that [grin]

But Roboguy suggested to me over #gamedev that I should try templates:
template<typename Type>int SetFlag(int ID, Type value) {	switch (ID) {		case FLAG_FRAMEFUNC:			DoFrame = value;		// ...	}}


In effect, I think this is pretty much the same thing as your code, just less of it. Mabey your version is more high performance :)

The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group
I've tried templates before and I get errors (for my different switch, saying that it can't convert from blah to blahblah, even though that case isn't being used.) This might be my dinosaur compiler (MSVC++6), I'm not sure. Either way, hope you get it working!
Try this
thefinalpointerto func=(int (*)())name_of_func;
Quote:Original post by The Forgotten Mindset
Hey guys, I've got a little function pointer conversion problem. Here's a simplified version of what I'm doing:

*** Source Snippet Removed ***

Alright, I know there are other ways of doing this, but I like this approach the best. And I don't think switches are that bad either [smile].

PS: Does anyone know how to change the win32 titlebar text at runtime? [help]


What exactly are you trying to do? Why can't you just override SetFlags() for different types or better yet, use entirely different functions to set different attributes. Ie. SetFrameFunction() for frame functions and SetWindowTitle() for window titles and so on. There are most likely much better ways of doing what you're trying to do. In C++ there is rarely a need to use function pointers at all, since virtual methods are often better suited for the job. (Functors or function objects are a different matter entirely.)
Yes! Thanks vallentin that's exactly what I was looking for! It works now!

And to answer your question FlowingOoze, I was using functions like SetFrameFunc(), but I like this approach better. It allows every single command to be processed in one central function. But overloading that function would have been a good choice too. Thanks :).

Rating++es for all! [smile]
The G'Bro GameDev Society! -The Southeastern US GameDev Gathering Group

This topic is closed to new replies.

Advertisement