Can we pass a member function in a class as a callback function?
You can pass whatever the interface requires, and nothing else.
I've even seen some interfaces (in a Windows manager for Sun)
which used pointers to member function for the callbacks. It's
rather exceptional, however. Typically, two cases occur:
-- The interface was designed with C++ in mind. In that case,
most of the time, you pass pointers to functional objects as
the callback. Traditionally, those objects must derive from
a common base class, and override a specific function in
that class, but some newer interfaces use more or less fancy
template tricks to allow you to pass pretty much anything
which will support a function call operator.
-- The interface is designed for C or C/C++. In that case,
you'll almost certainly have to pass the address of an
`extern "C"' function: static members don't cut it, nor do
normal global functions in C++. Hopefully, you'll also get
the chance to pass a void* with the address of "user data";
your user data will be a functional object, as above, which
gets called in the function whose address you pass.
Someone instucted me that I can only use a static functon or a global
function as a callback.
To date, I've never seen an interface where a static member
function could be used (except perhaps some of those using fancy
templates). See above: it very much depends on the interface,
and it's possible for an interface to use just about anything.
What you do have to do is conform to the interface: if it
expects a pointer to a member function of class X, you have to
pass it a pointer to a (non-static) member fucntion of class X.
If it has a C compatible interface, you must pass it a function
declared `extern "C"'. And if it expects a pointer to a class
CallBack, you have to derive from CallBack, and pass it a
pointer to an object of the derived type.