{"id":40683,"date":"2004-02-10T07:00:00","date_gmt":"2004-02-10T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/02\/10\/answer-to-exercise-pointer-to-member-function-cast\/"},"modified":"2004-02-10T07:00:00","modified_gmt":"2004-02-10T07:00:00","slug":"answer-to-exercise-pointer-to-member-function-cast","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040210-00\/?p=40683","title":{"rendered":"Answer to exercise: Pointer to member function cast"},"content":{"rendered":"<p>\n<a HREF=\"http:\/\/weblogs.asp.net\/oldnewthing\/archive\/2004\/02\/09\/70002.aspx\">\nYesterday&#8217;s exercise<\/a> asked you to predict and explain the\ncodegen for the following fragment:<\/p>\n<pre>\nclass Base1 { int b1; void Base1Method(); };\nclass Base2 { int b2; void Base2Method(); };\nclass Derived : public Base1, Base2\n  { int d; void DerivedMethod(); };\nclass Derived2 : public Base3, public Derived { };\nvoid (Derived::*pfnDerived)();\nvoid (Derived2::*pfnDerived2();\npfnDerived2 = pfnDerived;\n<\/pre>\n<p>\nWell, the codegen might go something like this:\n<\/p>\n<pre>\n  mov  ecx, pfnDerived[0]       ; ecx = address\n  mov  pfnDerived2[0], ecx\n  mov  ecx, pfnDerived2[4]      ; ecx = adjustor\n  add  ecx, sizeof(Base3)       ; adjust the adjustor!\n  mov  pfnDerived2[4], ecx\n<\/pre>\n<p>\nLet&#8217;s use one of our fancy pictures:\n<\/p>\n<table BORDER=\"0\" CELLSPACING=\"0\">\n<col SPAN=\"3\" ALIGN=\"center\">\n<tr>\n<td valign=\"top\">p<\/td>\n<td valign=\"top\">&rarr;<\/td>\n<td STYLE=\"border: solid 1px buttonshadow;border-bottom: none;width: 10em\">\n<div STYLE=\"border: solid 1px buttonshadow;margin: .5ex\">\n       Base3::b3\n     <\/div>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">q<\/td>\n<td valign=\"top\">&rarr;<\/td>\n<td STYLE=\"border: solid 1px buttonshadow;border-top: none;width: 10em\">\n<div STYLE=\"border: solid 1px buttonshadow;margin: .5ex\">\n<div STYLE=\"border: solid 1px buttonshadow;margin: .5ex\">\n        Base2::b2\n       <\/div>\n<div STYLE=\"border: solid 1px buttonshadow;margin: .5ex\">\n        Base1::b1\n       <\/div>\n<p>       Derived::d\n     <\/p><\/div>\n<\/td>\n<\/tr>\n<\/table>\n<p>\nJust for fun, I swapped the order of Base1 and Base2.\nThere is no requirement in the standard about the order in which\nstorage is allocated for base classes, so the compiler is completely\nwithin its rights to put Base2 first, if it thinks that would be more\nefficient.\n<\/p>\n<p>\nA pointer to member function for class Derived expects the &#8220;this&#8221;\npointer to be at &#8220;q&#8221;.  So when we have a &#8220;p&#8221;, we need to add\nsizeof(Base3) to it to convert it to &#8220;q&#8221;, on top of whatever other\nadjustment the original function pointer wanted.\nThat&#8217;s why we add sizeof(Base3) to the existing adjustor to make\na new combined adjustor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yesterday&#8217;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(); [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-40683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Yesterday&#8217;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(); [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=40683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/40683\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=40683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=40683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=40683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}