quote:
I agree, but I'm wondering why GCC uses 64 bits. All you need to call a procedure is 32 bits, after all. Couldn't you use inline assembly language for compatibility? In reference to your previous remark, how do C COM interfaces work, then? When you call COM interfaces in C, you are forced to deal with explicitly calling them through their VTBLs and sending the THIS parameter in first.
I don't know the details of how pointers to non-static member functions are implemented in the two compilers, but I'm glad you brought that up. I'd been kinda curious about the size myself, so I wrote a little test program to investigate, and I discovered that Visual C++ actually switches from 32-bits to 64-bits when you use multiple inheritance! So you have no way of knowing the size of (Class::*memberFuncPtr)() without looking at the class definition -- yet another reason not to try any casting tricks.
As for why you'd ever need more than 32 bits, the missing "this" pointer is actually more important than it seems. Suppose we have the following code:
class Class{public: ... virtual void MemberFunction(); ...}; void Class::MemberFunction(){ ...} typedef void (Class:*memberFuncPtr)(); void CallClassFunction( Class* pClass, memberFuncPtr pFunc ){ if( pClass != NULL ) { (pClass->*pFunc)(); }} int main( void ){ Class myObject; ... CallClassFunction( &myObject, &Class::MemberFunction ); ...}
Notice that CallClassFunction takes a Class* argument. What if we create a subclass of Class, override MemberFunction(), then pass in (Class*)pSubclass? Whenever we call MemberFunction on a Subclass object, we expect to call Subclass::MemberFunction, so why should it be any different if we call MemberFunction through a non-static member function pointer? To keep the late-binding behavior of the virtual function, the non-static member function pointer can't simply contain the 32-bit address of Class::MemberFunction. To call the correct function, we need the correct vtbl, and we won't get that until we use the pointer with a specific object. No "this" pointer, no vtbl. Compiler writers need to pack enough information into a non-static member function pointer to find the correct function to call for any appropriate object. Apparently, the Visual C++ and gcc teams used slightly different approaches to the problem.
Fortunately, none of this applies to using COM interfaces in C. To access an interface, you need to know the address of a COM object so you can pass it as the "this" argument. You can get the vtbl from the "this" pointer, so all you need to do is choose your standard, 32-bit function pointer from the vtbl.
Non-static member function pointers may seem like more trouble than they're worth, but they're really not that bad as long as you don't do any inappropriate casting.
Edited by - EvilTwin on June 29, 2000 3:32:34 AM