Re: Callback Trouble
I am trying to implement callbacks in C++. I am modeling my callbacks
after some of the stuff that was done in wxWidgets (if anyone is
familiar with that). The syntax is as follows:
Please don't hide namespace declarations etc. in macros.
typedef void (PI_PluginAPI::*KMKeyboardMethod)(const KeyboardEvent
The less you use raw pointers, the better.
Pointers to member functions are especially bad (in general).
Try something like
virtual void onEvent( KeyboardEvent const& ) = 0;
class KM_Keymap : public PI_PluginAPI
Why use prefixes instead of namespaces?
// More functions follow ....
bool AddKeymapEntryMethod(const char *top_entry, const char
*name,const char *desc, PI_PluginAPI *obj, KMKeyboardMethod func, void
As mentioned raw pointers are bad, member function pointers especially
bad, and void* pointers, used above, are simply unacceptable in high
level code (if you must interface to C then that's another matter, but
the above doesn't).
// Private stuff here
Then I have a calling class. It structure is as follows:
class My_Plugin : virtual public NGMX::PI_PluginAPI
// More stuff ...
void ShowStuffCB(const NGMX::KeyboardEvent *event);
// Private stuff
To connect the callback I make this call in a method inside of the
My_Plugin (My_Plugin class uses the NGMX namespace up top) class:
// In source
m_keymap->AddKeymapEntryMethod("BASE","Show Stuff","Turn on and off
C style casts are bad.
Now the error I am getting is as follows (in Visual Studio 2003):
error C2664: 'NGMX::KM_Keymap::AddKeymapEntryMethod' : cannot convert
parameter 5 from 'void (__thiscall My_Plugin::* )(void)' to
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
The error message is not related to any code shown.
Reproduce the error in a smallest possible complete program and post the
code if that effort in itself doesn't help you.
See the FAQ for help about how to post a question about code that
doesn't work as expected.
So it appears that it doesn't like the &My_Plugin::ShowStuffCB
parameter. My_Plugin inherits from PI_PluginAPI so I don't know why
the cast doesn't work. I am not sure what I am doing wrong. Anyone see
something glaring that needs to be fixed? Thanks in advance for you
Cheers, & hth.,
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
Q: What is the most annoying thing on usenet and in e-mail?