{"id":995,"date":"2017-11-08T21:19:08","date_gmt":"2017-11-08T21:19:08","guid":{"rendered":"http:\/\/danielschlegel.org\/wp\/?page_id=995"},"modified":"2017-11-14T21:56:45","modified_gmt":"2017-11-14T21:56:45","slug":"diamond-problem","status":"publish","type":"page","link":"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/","title":{"rendered":"Diamond Problem"},"content":{"rendered":"<h2>The &#8220;Deadly Diamond of Death&#8221;<\/h2>\n<p>Consider a set of classes with the following inheritance hierarchy. <\/p>\n<pre> \r\n   A\r\n \/   \\\r\nB     C\r\n \\   \/\r\n   D\r\n<\/pre>\n<h3>diamond.cpp<\/h3>\n<pre class=\"lang:cpp\">\r\n#include<iostream>\r\nusing namespace std;\r\n\r\nclass A {\r\n\tint value;\r\n\r\npublic:\r\n\tA() {\r\n\t\tvalue = 5;\r\n\t\tcout << \"A's constructor called\" << endl;\r\n\t}\r\n\tvoid printValue(){\r\n\t\tcout << value << endl;\r\n\t}\r\n};\r\n\r\nclass B: public A {\r\npublic:\r\n\tB() {\r\n\t\tcout << \"B's constructor called\" << endl;\r\n\t}\r\n};\r\n\r\nclass C: public A {\r\npublic:\r\n\tC() {\r\n\t\tcout << \"C's constructor called\" << endl;\r\n\t}\r\n};\r\n\r\nclass D: public B, public C {\r\npublic:\r\n\tD() {\r\n\t\tcout << \"D's constructor called\" << endl;\r\n\t}\r\n};\r\n\r\nint main() {\r\n\tD d;\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>Outputs: <\/p>\n<pre>\r\nA's constructor called\r\nB's constructor called\r\nA's constructor called\r\nC's constructor called\r\nD's constructor called\r\n<\/pre>\n<p>Our class hierarchy ended up looking more like this: <\/p>\n<pre>\r\nA     A\r\n|     |\r\nB     C\r\n \\   \/\r\n   D\r\n<\/pre>\n<p>This is because C++ by default uses <em>replicated inheritance<\/em>. <\/p>\n<p>Let's investigate further. Now add printValue() to D's main: <\/p>\n<pre class=\"lang:cpp\">\r\nint main() {\r\n\tD d;\r\n\td.printValue();\r\n\treturn 0;\r\n}\r\n<\/pre>\n<p>Output:<\/p>\n<pre class=\"lang:cpp\">\r\n..\/diamond.cpp:47:4: error: non-static member 'printValue' found in multiple base-class subobjects of type 'A':\r\n    class D -> class B -> class A\r\n    class D -> class C -> class A\r\n        d.printValue();\r\n          ^\r\n..\/diamond.cpp:19:7: note: member found by ambiguous name lookup\r\n        void printValue(){\r\n             ^\r\n1 error generated.\r\n<\/pre>\n<p>The same thing will happen if we try to add<\/p>\n<pre class=\"lang:cpp\">\r\nA a = d;\r\n<\/pre>\n<p>to the main. We could try to tell the compiler which A we mean by saying something like: <\/p>\n<pre class=\"lang:cpp\">\r\nA a = static_cast<B>(d);\r\na.printValue();\r\n<\/pre>\n<p>which works, but doesn't solve the problem that we have two different A's. <\/p>\n<p>Solution: use <em>shared multiple inheritance<\/em> - make B and C inherit from virtual A. <\/p>\n<pre class=\"lang:cpp\">\r\nclass B: virtual public A { ... }\r\nclass C: virtual public A { ... }\r\n<\/pre>\n<pre>\r\nA's constructor called\r\nB's constructor called\r\nC's constructor called\r\nD's constructor called\r\n5\r\n<\/pre>\n<p>The actual machinery behind all of this is quite complex, but you can read about it a bit <a href=\"https:\/\/shaharmike.com\/cpp\/vtable-part3\/\">here<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"lead\">The &#8220;Deadly Diamond of Death&#8221; Consider a set of classes with the following inheritance hierarchy. A \/ \\ B C \\ \/ D diamond.cpp #include using namespace std; class A { int value; public: A() { value = 5; cout<\/p>\n<p class=\"more-link-p\"><a class=\"btn btn-warning\" href=\"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":11,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":0,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","footnotes":""},"class_list":["post-995","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Diamond Problem - Daniel R. Schlegel<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Diamond Problem - Daniel R. Schlegel\" \/>\n<meta property=\"og:description\" content=\"The &#8220;Deadly Diamond of Death&#8221; Consider a set of classes with the following inheritance hierarchy. A \/  B C  \/ D diamond.cpp #include using namespace std; class A { int value; public: A() { value = 5; coutRead more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/\" \/>\n<meta property=\"og:site_name\" content=\"Daniel R. Schlegel\" \/>\n<meta property=\"article:modified_time\" content=\"2017-11-14T21:56:45+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/teaching\\\/diamond-problem\\\/\",\"url\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/teaching\\\/diamond-problem\\\/\",\"name\":\"Diamond Problem - Daniel R. Schlegel\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/#website\"},\"datePublished\":\"2017-11-08T21:19:08+00:00\",\"dateModified\":\"2017-11-14T21:56:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/teaching\\\/diamond-problem\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/teaching\\\/diamond-problem\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/teaching\\\/diamond-problem\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Teaching\",\"item\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/teaching\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Diamond Problem\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/#website\",\"url\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/\",\"name\":\"Daniel R. Schlegel\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/danielschlegel.org\\\/wp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Diamond Problem - Daniel R. Schlegel","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/","og_locale":"en_US","og_type":"article","og_title":"Diamond Problem - Daniel R. Schlegel","og_description":"The &#8220;Deadly Diamond of Death&#8221; Consider a set of classes with the following inheritance hierarchy. A \/  B C  \/ D diamond.cpp #include using namespace std; class A { int value; public: A() { value = 5; coutRead more","og_url":"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/","og_site_name":"Daniel R. Schlegel","article_modified_time":"2017-11-14T21:56:45+00:00","twitter_card":"summary_large_image","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/","url":"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/","name":"Diamond Problem - Daniel R. Schlegel","isPartOf":{"@id":"https:\/\/danielschlegel.org\/wp\/#website"},"datePublished":"2017-11-08T21:19:08+00:00","dateModified":"2017-11-14T21:56:45+00:00","breadcrumb":{"@id":"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/danielschlegel.org\/wp\/teaching\/diamond-problem\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/danielschlegel.org\/wp\/"},{"@type":"ListItem","position":2,"name":"Teaching","item":"https:\/\/danielschlegel.org\/wp\/teaching\/"},{"@type":"ListItem","position":3,"name":"Diamond Problem"}]},{"@type":"WebSite","@id":"https:\/\/danielschlegel.org\/wp\/#website","url":"https:\/\/danielschlegel.org\/wp\/","name":"Daniel R. Schlegel","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/danielschlegel.org\/wp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P83Tb6-g3","_links":{"self":[{"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/pages\/995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/comments?post=995"}],"version-history":[{"count":11,"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/pages\/995\/revisions"}],"predecessor-version":[{"id":1035,"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/pages\/995\/revisions\/1035"}],"up":[{"embeddable":true,"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/pages\/11"}],"wp:attachment":[{"href":"https:\/\/danielschlegel.org\/wp\/wp-json\/wp\/v2\/media?parent=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}