Pass callback function if it is a class member

Aug 21, 2008 at 2:46pm
I have a class CUsersView which is derived from CTreeView.
In OnUpdate function I want to sort root elements using custom callback function:

Callback function:

.h

1
2
3
4
5
class CUsersView : public CDNDTreeView
{
	int CALLBACK UserGroupCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
...
}


.cpp

1
2
3
4
5
int CALLBACK 
CUsersView::UserGroupCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	// Some code, including using of CUsersView instance members
}


Sorting:

1
2
3
4
TVSORTCB tvs;
tvs.hParent = hTreeItem;
tvs.lpfnCompare = UserGroupCompareProc;
GetTreeCtrl().SortChildrenCB(&tvs);


I receive error:

error C2440: '=' : cannot convert from 'int (__stdcall CUsersView::* )(LPARAM,LPARAM,LPARAM)' to 'PFNTVCOMPARE'
There is no context in which this conversion is possible


PFNTVCOMPARE is:

typedef int (CALLBACK *PFNTVCOMPARE)(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);

I suppose I pass function pointer incorrectly...

Thanks!
Aug 21, 2008 at 5:46pm
Non-static object methods cannot be used directly as callback functions.

Here are a couple of pages that deal with creating callback adaptors.
http://www.partow.net/programming/templatecallback/index.html
http://www.myelin.co.nz/notes/callbacks/cpp-interface-callbacks.html

Good luck!
Aug 21, 2008 at 7:32pm
Non-static object methods cannot be used directly as callback functions.


Don't confuse this statement with not being able to use function pointers to object methods. That is possible. Unfortunately, the interface your dealing with doesn't support it.
Topic archived. No new replies allowed.