typedef int (*Functor)();Functor doIntro;Functor doFrame;Functor doOutro;// ...doFrame = reinterpret_cast<Functor>(value);
void* to functor casting???
Try:
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:
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 :)
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 :)
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!
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]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement