TITLE: the type of pointer-to-member-functions (Newsgroups: comp.lang.c++.moderated, 30 Apr 97) CONYNGHAM: Jim Conyngham > > The C++ language doesn't have separate types for "pointer to virtual member > function" and "pointer to non-virtual member function". There are only > "pointer to member function" types. Since these types can be assigned > pointers to non-virtual member functions, a pointer-to-member-function > must be implemented as the address of code that can be called directly. > The difference between a "pointer to member function" and just a "pointer > to function" is that dereferencing the former pushes the hidden "this" > argument onto the stack. CLAMAGE: Steve Clamage That isn't quite right You are correct that C++ does not distinguish between "pointer to non-virtual member function" and "pointer to virtual member function". When you call a member function function via such a pointer, you get virtual dispatch if the pointed-to function is virtual, and a normal member function call if not. The rule could not realistically be otherwise. Thus, the implementation of the pointer-to-member-function (PTMF) must contain enough information to work with both virtual and non-virtual functions. And that is part of the reason why PTMFs and ordinary function pointers are completely incompatible. A PTMF is thus a structure, not a simple type.It needs to indicate whether the function is virtual. For virtual functions, the structure contains the function index, or whatever is used to identify a virtual function. For non-virtual functions, the structure contains the address of the function. In the current Sun implemenation, for example, virtual function indexes are never less than 1. The first word in the PTMF structure contains the virtual function index or 0. The second word contains 0 or the address of the non-virtual function. When you assign to a PTMF, the compiler sets the structure fields according to the kind of function. When you call a function via a PTMF, the structure is examined at run time and the proper form of function call occurs.