Answer to exercise: Pointer to member function cast

Date:February 10, 2004 / year-entry #54
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20040210-00/?p=40683
Comments:    3
Summary:Yesterday's exercise asked you to predict and explain the codegen for the following fragment: class Base1 { int b1; void Base1Method(); }; class Base2 { int b2; void Base2Method(); }; class Derived : public Base1, Base2 { int d; void DerivedMethod(); }; class Derived2 : public Base3, public Derived { }; void (Derived::*pfnDerived)(); void (Derived2::*pfnDerived2();...

Yesterday's exercise asked you to predict and explain the codegen for the following fragment:

class Base1 { int b1; void Base1Method(); };
class Base2 { int b2; void Base2Method(); };
class Derived : public Base1, Base2
  { int d; void DerivedMethod(); };
class Derived2 : public Base3, public Derived { };

void (Derived::*pfnDerived)();
void (Derived2::*pfnDerived2();

pfnDerived2 = pfnDerived;

Well, the codegen might go something like this:

  mov  ecx, pfnDerived[0]       ; ecx = address
  mov  pfnDerived2[0], ecx

  mov  ecx, pfnDerived2[4]      ; ecx = adjustor
  add  ecx, sizeof(Base3)       ; adjust the adjustor!
  mov  pfnDerived2[4], ecx

Let's use one of our fancy pictures:

p
Base3::b3
q
Base2::b2
Base1::b1

Derived::d

Just for fun, I swapped the order of Base1 and Base2. There is no requirement in the standard about the order in which storage is allocated for base classes, so the compiler is completely within its rights to put Base2 first, if it thinks that would be more efficient.

A pointer to member function for class Derived expects the "this" pointer to be at "q". So when we have a "p", we need to add sizeof(Base3) to it to convert it to "q", on top of whatever other adjustment the original function pointer wanted. That's why we add sizeof(Base3) to the existing adjustor to make a new combined adjustor.


Comments (3)
  1. asdf says:

    you’re missing:

    class Base3 { int b3; void Base3Method(); };

  2. Jack Mathews says:

    Now you need to get into the evils of good old virtual inheritance. :)

  3. Raymond Chen says:

    I am so not going to cover virtual inheritance. That way lies madness.

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index