Is it possible to have callback function as a class member?
-
Hi, all :) I am writing a class that creates and maintains a window/control. The problem is that I can’t seem to give the class its own private callback function (the Window Procedure alias WndProc); the VC++ complier simply gives a type cast error when giving out the function pointer. I thought about why and I assume that the reason lies in the different structure between class functions and global functions. But is there a way to go round this, like force the compiler to construct the class function as a global function (assuming that’s the case)? If that’s not that the case, then why does the compiler give a type cast error? And is it possible to have callback function as a class member or at least give the class some sort of private callback function? And oh yes, I know I can use MFC but I simply prefer not, I want to know if can be done without its help. Thanks in advance Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
-
Hi, all :) I am writing a class that creates and maintains a window/control. The problem is that I can’t seem to give the class its own private callback function (the Window Procedure alias WndProc); the VC++ complier simply gives a type cast error when giving out the function pointer. I thought about why and I assume that the reason lies in the different structure between class functions and global functions. But is there a way to go round this, like force the compiler to construct the class function as a global function (assuming that’s the case)? If that’s not that the case, then why does the compiler give a type cast error? And is it possible to have callback function as a class member or at least give the class some sort of private callback function? And oh yes, I know I can use MFC but I simply prefer not, I want to know if can be done without its help. Thanks in advance Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
I want to help you but I know just a little about callback function I ever solved a callback function as a private static member function on the base of other people in VC, which perhaps develops from MFC It works well.
-
Hi, all :) I am writing a class that creates and maintains a window/control. The problem is that I can’t seem to give the class its own private callback function (the Window Procedure alias WndProc); the VC++ complier simply gives a type cast error when giving out the function pointer. I thought about why and I assume that the reason lies in the different structure between class functions and global functions. But is there a way to go round this, like force the compiler to construct the class function as a global function (assuming that’s the case)? If that’s not that the case, then why does the compiler give a type cast error? And is it possible to have callback function as a class member or at least give the class some sort of private callback function? And oh yes, I know I can use MFC but I simply prefer not, I want to know if can be done without its help. Thanks in advance Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
Boost can let you do this. Here's a starting point, but for a much better example download boost and look at the sample code for boost::thread http://www.codeproject.com/vcpp/stl/boostintro.asp#xx563708xx[^]
If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling
-
Hi, all :) I am writing a class that creates and maintains a window/control. The problem is that I can’t seem to give the class its own private callback function (the Window Procedure alias WndProc); the VC++ complier simply gives a type cast error when giving out the function pointer. I thought about why and I assume that the reason lies in the different structure between class functions and global functions. But is there a way to go round this, like force the compiler to construct the class function as a global function (assuming that’s the case)? If that’s not that the case, then why does the compiler give a type cast error? And is it possible to have callback function as a class member or at least give the class some sort of private callback function? And oh yes, I know I can use MFC but I simply prefer not, I want to know if can be done without its help. Thanks in advance Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
This is standard C++ stuff which you should find in any good C++ Reference book. Here is a code snippet:
class CBookmarks { public: void ScanBookmarks( void (CBookmarks::*Scan_Func)( const BMRK, const ulong, const ulong ), const ulong var1, const ulong var2, const LN_NUM ln_num ); }; // Function definition void CBookmarks::ScanBookmarks( void (CBookmarks::*Scan_Func)( const BMRK, const ulong, const ulong ), const ulong var1, const ulong var2, const LN_NUM ln_num ) { } // Function using callback. bits CBookmarks::ClmChanged( const LN_NUM ln_num, const LN_CLM clm, const int dir ) { m_changed_bookmarks = 0x00; ScanBookmarks( _ClmChanged, clm, dir, ln_num ); return m_changed_bookmarks; } // Callback function void CBookmarks::_ClmChanged( const BMRK mp, const LN_CLM clm, const ulong dir ) { if ( mp->m_ln_clm >= clm ) { mp->m_ln_clm += dir; MASKON( mp->m_status, MOVED ); // flag that the bookmark has moved BITON( m_changed_bookmarks, mp - m_bookmarks ); mp->m_ln_oclm = MARK_OFF; // reset as no longer valid } }
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com -
I want to help you but I know just a little about callback function I ever solved a callback function as a private static member function on the base of other people in VC, which perhaps develops from MFC It works well.
Thanks that worked (making the function static), but another problem came up. I get a link error saying “illegal reference to data member 'Class::X' in a static member function”, whenever the static function uses other class members (both variables and other functions). How can I solve this? :( Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
-
Thanks that worked (making the function static), but another problem came up. I get a link error saying “illegal reference to data member 'Class::X' in a static member function”, whenever the static function uses other class members (both variables and other functions). How can I solve this? :( Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
Usually you can pass a pointer on your main window to the callback function. If the parameter of the callback function is LPVOID, you will have to typecast it to your window type. if it looks like
CMyDialog::callbackfunction(... , LPVOID pointer)
use it like this :
callbackfuntionc(...,this);
and in the function
CMyDialog::callbackfunction(... , LPVOID pointer)
{
CMyDialog *pMain=(CMyDialog*)pointer;
pMain->m_variable=0; // here you can access them...
}~RaGE();
-
I want to help you but I know just a little about callback function I ever solved a callback function as a private static member function on the base of other people in VC, which perhaps develops from MFC It works well.
You can define a static varible member in the class including callback function if something has happened you can change the varible of course it is an indirect way. :)
-
Thanks that worked (making the function static), but another problem came up. I get a link error saying “illegal reference to data member 'Class::X' in a static member function”, whenever the static function uses other class members (both variables and other functions). How can I solve this? :( Aidman » over and out We haven't inherited Earth from our parents, instead we have borrowed her from our children; an old Indian saying.
static function can not access non-static members. Look at the Code Project FAQs for anouther way to implement callback functions. INTP