F
Francesco S. Carta
They don't have to belong to the same class, although the
classes do have to be related by inheritance.
And there's no real "offset" involved. There are two common
techniques for implementing them: in the most obvious, the
pointer stores either the actual address of the function (if
non-virtual) or the index into the vtable (if virtual), some
means of distinguishing between the two, and any correction
necessary to the this pointer. Alternatively, the compiler
generates a trampoline function, which does whatever is
necessary, and stores a pointer to it.
I knew my description was imperfect at least ;-)
Thanks for the correction.
A more frequent approach to this problem involves using an
instance of a forwarding class.
The most frequent use of pointers to member functions is in
instantiating templates, where the pointer to member function is
in fact resolved at compile time.
Would you please post a small example or a pointer to some resource?
I've searched for "forwarding class" but I just found stuff about
forward declarations... is that a pattern?
Also the usage of pointers to member functions in order to instantiate
templates is unknown to me, a small snippet (or again a reference) to
realize what it is all about would be more than welcome.
Some early GUI frameworks made extensive use of them for
callbacks.
I'm starting to suspect there is something wrong with my fixation with
interfaces, GUIs and moreover with the fact that there are a lot of "old
ideas" in my mind even though I never really inspected or studied old
pieces of code... maybe that's just the "normal level" one comes to when
trying to work things out by oneself.