Function references
-
I am maintaing an application with mixed code. Some is C++ and follows the OO paradigm some it dates back 1980 and follows the standard C style. I am trying to get function references from member functions to assign them into old fashion function pointers. If I can do I may have a listeners pattern in the old code. I tried a small example like
#include #include typedef int TBK_BOOL; #define TBK_TRUE 1 #define TBK_FALSE 0 typedef void (*TB_LISTENER_NOTIFICATION_FUNCTION)( TBK_BOOL value); class Listener { private: int identity; public: Listener() {identity = 0; }; void setId(int id) { identity = id; }; int getId(int id) { return identity;}; void notify(TBK_BOOL note); }; void Listener::notify( TBK_BOOL note) { if( note ) printf("Object no %d was notified\n", this->identity); else printf("Object no %d was left asleep \n", this->identity); } int main(int argc, LPCSTR argv []) { Listener x; Listener y; TB_LISTENER_NOTIFICATION_FUNCTION lists[2]; x.getId(1); y.getId(2); // Try to assing the object functions to an old fashion member lists[0] = x.notify; // line 45 lists[1] = y.notify; // line 46 (lists[0])(TBK_FALSE); (lists[1])(TBK_TRUE); }
And I received the following compilation errors G:\func.cpp(45) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible G:\func.cpp(46) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible Is anyway that I can do what I am trying or should I abandon the idea of referencing object functions outside of it?? -
I am maintaing an application with mixed code. Some is C++ and follows the OO paradigm some it dates back 1980 and follows the standard C style. I am trying to get function references from member functions to assign them into old fashion function pointers. If I can do I may have a listeners pattern in the old code. I tried a small example like
#include #include typedef int TBK_BOOL; #define TBK_TRUE 1 #define TBK_FALSE 0 typedef void (*TB_LISTENER_NOTIFICATION_FUNCTION)( TBK_BOOL value); class Listener { private: int identity; public: Listener() {identity = 0; }; void setId(int id) { identity = id; }; int getId(int id) { return identity;}; void notify(TBK_BOOL note); }; void Listener::notify( TBK_BOOL note) { if( note ) printf("Object no %d was notified\n", this->identity); else printf("Object no %d was left asleep \n", this->identity); } int main(int argc, LPCSTR argv []) { Listener x; Listener y; TB_LISTENER_NOTIFICATION_FUNCTION lists[2]; x.getId(1); y.getId(2); // Try to assing the object functions to an old fashion member lists[0] = x.notify; // line 45 lists[1] = y.notify; // line 46 (lists[0])(TBK_FALSE); (lists[1])(TBK_TRUE); }
And I received the following compilation errors G:\func.cpp(45) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible G:\func.cpp(46) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible Is anyway that I can do what I am trying or should I abandon the idea of referencing object functions outside of it??as
notify()
is a member of a class, then it implicitely receives thethis
pointer as a parameter... to solve this, declare it like this :static void notify(TBK_BOOL note);
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
I am maintaing an application with mixed code. Some is C++ and follows the OO paradigm some it dates back 1980 and follows the standard C style. I am trying to get function references from member functions to assign them into old fashion function pointers. If I can do I may have a listeners pattern in the old code. I tried a small example like
#include #include typedef int TBK_BOOL; #define TBK_TRUE 1 #define TBK_FALSE 0 typedef void (*TB_LISTENER_NOTIFICATION_FUNCTION)( TBK_BOOL value); class Listener { private: int identity; public: Listener() {identity = 0; }; void setId(int id) { identity = id; }; int getId(int id) { return identity;}; void notify(TBK_BOOL note); }; void Listener::notify( TBK_BOOL note) { if( note ) printf("Object no %d was notified\n", this->identity); else printf("Object no %d was left asleep \n", this->identity); } int main(int argc, LPCSTR argv []) { Listener x; Listener y; TB_LISTENER_NOTIFICATION_FUNCTION lists[2]; x.getId(1); y.getId(2); // Try to assing the object functions to an old fashion member lists[0] = x.notify; // line 45 lists[1] = y.notify; // line 46 (lists[0])(TBK_FALSE); (lists[1])(TBK_TRUE); }
And I received the following compilation errors G:\func.cpp(45) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible G:\func.cpp(46) : error C2440: '=' : cannot convert from 'void (__thiscall Listener::*)(int)' to 'void (__cdecl *)(int)' There is no context in which this conversion is possible Is anyway that I can do what I am trying or should I abandon the idea of referencing object functions outside of it??You can't (as you've seen) assign a pointer to a member function to a normal function pointer. A pointer to a member function would look like this:
typedef void (Listener::*)(TBK_BOOL note) NEW_LISTENER_NOTIFICATION_FUNCTION;
To call this pointer you would need more information then with a normal function pointer, you need to supply thethis
pointer. A call could look like this:Listener x; NEW_LISTENER_NOTIFICATION_FUNCTION pFunc = &Listener::notify; (x.*pFunc)(Some_Value);
The functionality you're after seems more like a delegate then a C++ style member pointer. I believe there is an article on the code project which provides such delegates - but these can only be used from C++. Steve -
You can't (as you've seen) assign a pointer to a member function to a normal function pointer. A pointer to a member function would look like this:
typedef void (Listener::*)(TBK_BOOL note) NEW_LISTENER_NOTIFICATION_FUNCTION;
To call this pointer you would need more information then with a normal function pointer, you need to supply thethis
pointer. A call could look like this:Listener x; NEW_LISTENER_NOTIFICATION_FUNCTION pFunc = &Listener::notify; (x.*pFunc)(Some_Value);
The functionality you're after seems more like a delegate then a C++ style member pointer. I believe there is an article on the code project which provides such delegates - but these can only be used from C++. Steveas his function doesn't modify the class itself, he'd better set the function static...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...] -
as his function doesn't modify the class itself, he'd better set the function static...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...]It doesn't modify the class but it does access a member variable. I assumed that in the real implementation the function would be more elaborate. Steve
-
It doesn't modify the class but it does access a member variable. I assumed that in the real implementation the function would be more elaborate. Steve
:-O sorry, i didn't see the
this->identity
...
TOXCCT >>> GEII power
[toxcct][VisualCalc 2.20][VCalc 3.0 soon...]