Date: | March 24, 2005 / year-entry #74 |
Tags: | code |
Orig Link: | https://blogs.msdn.microsoft.com/oldnewthing/20050324-00/?p=36093 |
Comments: | 8 |
Summary: | As a mental exercise, let's combine two mind-numbing facts about pointers to member functions, namely that all pointers to virtual functions look the same and that pointers to member functions are very strange animals. The result may make your head explode. Consider: class Class1 { public: virtual int f() { return 1; } }; class... |
As a mental exercise, let's combine two mind-numbing facts about pointers to member functions, namely that all pointers to virtual functions look the same and that pointers to member functions are very strange animals. The result may make your head explode. Consider: class Class1 { public: virtual int f() { return 1; } }; class Class2 { public: virtual int g() { return 2; } }; class Class3 : public Class1, public Class2 { }; int (Class3::*pfn)() = Class3::g;
Here, the variable mov eax, [ecx] ; first vtable jmp dword ptr [eax] ; first function
and the adjustor is mov ecx, p lea eax, pfn add ecx, dword ptr [eax+4] ; adjust call dword ptr [eax] ; call -- transfers to mov eax, [ecx] ; first vtable jmp dword ptr [eax] ; first function -- transfers to mov eax, 2 ; return 2 ret Okay, I lied. It's really not all that complicated after all. But you can probably still impress your friends with this knowledge. (If you have really geeky friends.) |
Comments (8)
Comments are closed. |
Surely you mean "May make your head a-splode"
Didn’t you do this one before? http://blogs.msdn.com/oldnewthing/archive/2004/02/06/68695.aspx
The pointer can be larger than that depending on the compiler options used (see http://www.codeproject.com/cpp/FastDelegate.asp for a good discussion). Also, the only way to get the address of a member is &Class3::g. The other ways Class3::g and &(Class3::g) are explicitly not allowed in the C++ standard.
asdf:
I knew about Class3::g being illegal but I’d never thought about trying &(Class3::g) with the parenthesis like that. Why is that illegal? Is it something with Class3::g not being a proper reference on its own or something?
http://groups-beta.google.com/group/comp.std.c++/msg/48771ca25d67df10
How come my keyboard’s asdf keys are still working and it’s the jkl; keys that got barfed on? Not fair!
Sure asdf, I know it’s not your fault, but you should have warned us not to look at that link after eating.
Hmm. Anyone need a slightly used copy of INCITS+ISO+IEC+14882-2003.pdf for a nice low cheap price?
That was adjustor *thunks*, which are related to but not the same as pointer-to-member-function adjustors.
Err. May be I missed something but I believe this ANSI document should be a good substitute for ISO 14882:2003
http://webstore.ansi.org/ansidocstore/product.asp?sku=INCITS%2FISO%2FIEC+14882-2003
And it is only $18 for PDF.