473,395 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

// Header
DEFINE_NAMESPACE_BEGIN(NGMX)

typedef void (PI_PluginAPI::*KMKeyboardMethod)(const KeyboardEvent
*event);

class KM_Keymap : public PI_PluginAPI
{
public:

KM_Keymap();

// More functions follow ....

bool AddKeymapEntryMethod(const char *top_entry, const char
*name,const char *desc, PI_PluginAPI *obj, KMKeyboardMethod func, void
*userdata=NULL);

private:

// Private stuff here
};
DEFINE_NAMESPACE_END

Then I have a calling class. It structure is as follows:

// Header
class My_Plugin : virtual public NGMX::PI_PluginAPI
{
public:
My_Plugin();

// More stuff ...

void ShowStuffCB(const NGMX::KeyboardEvent *event);

private:
// 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
Stuff",this,(KMKeyboardMethod)&My_Plugin::ShowStuf fCB);

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
'NGMX::KMKeyboardMethod'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast

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
help.
Nov 17 '07 #1
3 1469
On Fri, 16 Nov 2007 19:26:59 -0800 (PST), Nashirak wrote:
>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:
[...]
>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
'NGMX::KMKeyboardMethod'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
AFAICS, the problem is that member function pointers are not
polymorphic in C++. The following is a Reader's Digest version of your
code with some additions:

class KeyboardEvent;

class PI_PluginAPI {

};

typedef
void (PI_PluginAPI::*KMKeyboardMethod)(const KeyboardEvent *event);

class KM_Keymap : public PI_PluginAPI {
public:
bool AddKeymapEntryMethod(PI_PluginAPI *obj, KMKeyboardMethod func);
};
class My_Plugin : virtual public PI_PluginAPI
{
public:
void ShowStuffCB(const KeyboardEvent *event);

void foo() {
// works: calling member function via member function pointer
((*this).*(&My_Plugin::ShowStuffCB))(0);

// doesn't work: what you try to do would be similar to
// the following
PI_PluginAPI* pApi = this;
((*pApi).*(&My_Plugin::ShowStuffCB))(0);

// see error message: a pointer to a (non-static) derived class
// member function is not an pointer to a base memebr class
// function
m_keymap.AddKeymapEntryMethod(this, &My_Plugin::ShowStuffCB);
}

private:
KM_Keymap m_keymap;
};

BTW, a good callback library can be found here:
http://www.codeproject.com/cpp/FastDelegate.asp
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Nov 17 '07 #2
On Nov 17, 9:49 am, rpbg...@yahoo.com (Roland Pibinger) wrote:
AFAICS, the problem is that member function pointers are not
polymorphic in C++. The following is a Reader's Digest version of your
code with some additions:
Thanks for your responses. I played around with this a little more
this weekend and found that once I made My_Plugin inherit from
PI_PluginAPI in a non-virtual manner, everthing seemed to work and
compile. So by changing:

class My_Plugin : virtual public PI_PluginAPI

to

class My_Plugin : public PI_PluginAPI

Everything seemed to work ok.
Thanks again for your responses. I will take a look at that callback
library.
Nov 19 '07 #3
Nashirak wrote:
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:
// Header
DEFINE_NAMESPACE_BEGIN(NGMX)
Do you really still have to deal with compilers which don't
support namespaces?
typedef void (PI_PluginAPI::*KMKeyboardMethod)(const KeyboardEvent
*event);
class KM_Keymap : public PI_PluginAPI
{
public:
KM_Keymap();
// More functions follow ....
bool AddKeymapEntryMethod(const char *top_entry, const char
*name,const char *desc, PI_PluginAPI *obj, KMKeyboardMethod func, void
*userdata=NULL);
private:
// Private stuff here
};
DEFINE_NAMESPACE_END
Then I have a calling class. It structure is as follows:
// Header
class My_Plugin : virtual public NGMX::PI_PluginAPI
{
public:
My_Plugin();
// More stuff ...
void ShowStuffCB(const NGMX::KeyboardEvent *event);
private:
// 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
Stuff",this,(KMKeyboardMethod)&My_Plugin::ShowStuf fCB);
As has been pointed out, this type conversion isn't allowed if
the base is virtual.

More generally, although well established "existing practice",
it's a bad existing practice, and is not the way I would
implement callbacks. Since you have to inherit from a specific
base class anyway, just define a pure virtual function in it,
and use that for the callback. Something like:

class PluginAPI
{
public:
virtual ~PluginAPI() {}
// You might want to use a reference parameter here,
// since I'd be very surprised that you want to support
// calling this function with a null pointer.
virtual void notifyKeyboardEvent(
KeyboardEvent const* event ) = 0 ;
// ...
} ;

Given this, you can add a class template which derives from it,
and forwards to an arbitrary function in an arbitrary class, but
it's generally not worth the bother.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Pote | last post by:
Running my programme in Python 2.3.4 I received the following msg in the consol :- (Pent III running W2K prof) """ Exception in Tkinter callback Traceback (most recent call last): File...
6
by: Eric Entressangle | last post by:
Hi all, is there any trouble setting an C++ static class method as callback function for a C program or library ? Thanks
8
by: Ash | last post by:
Hello all, I am hoping this is the appropriate newsgroup for a C++ interface design question. I am trying to design an interface for a subscriber to register/deregister handlers for various...
6
by: ma740988 | last post by:
I was perusing modern C++ designs and the loki library and I might add I hit overload when I hit select part of that book and the library. The template _stuff_ is very intense ...... whew!! Oso...
7
by: Rich Claxton | last post by:
Hi we have a 3rd party in process com dll, which was written In C++ and our client is in c#. All is working fine apart from we get a callback from the DLL which contains an array of structures....
2
by: David | last post by:
Hi all, I am new to .Net environment. I have created a flat non-COM DLL from Visual C++ 6.0. It stores up a function pointer from caller, create a worker thread via WIN32 API, and then call...
2
by: dvestal | last post by:
I'm new to .NET remoting, and I'm having trouble getting a callback to work from a server. I've been through a dozen webpages and the references available to me, but I'm still missing something. ...
2
by: Nathan Sokalski | last post by:
I have a DataList in which the ItemTemplate contains two Button controls that use EventBubbling. When I click either of them I receive the following error: Server Error in '/' Application....
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.