{"id":19310,"date":"2025-09-23T13:05:23","date_gmt":"2025-09-23T13:05:23","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=19310"},"modified":"2025-09-23T13:13:10","modified_gmt":"2025-09-23T13:13:10","slug":"virtual-inheritance-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=19310","title":{"rendered":"Virtual inheritance (C++)"},"content":{"rendered":"<p>You need virtual inheritance to solve the diamond problem with multiple inheritance.<\/p>\n<hr>\n<p><strong>Not working version<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nstruct A {\r\n    int val;\r\n    A() : val(42) {}\r\n};\r\n\r\nstruct B : A {};\r\nstruct C : A {};\r\n\r\nstruct D : B, C {}; \/\/ D has now A from B and C\r\n\r\nint main() {\r\n    D d; \/\/ Error\r\n}\r\n<\/pre>\n<pre>\r\n          A\r\n        \/   \\\r\n      B       C\r\n        \\   \/\r\n          D\r\n<\/pre>\n<hr>\n<p><strong>Working version<\/strong><\/p>\n<pre class=\"brush: cpp; gutter: false; title: ; notranslate\" title=\"\">\r\nstruct A {\r\n    int val;\r\n    A() : val(42) {}\r\n};\r\n\r\nstruct B : virtual A {};\r\nstruct C : virtual A {};\r\n\r\nstruct D : B, C {}; \/\/ D now has just one A\r\n\r\nint main() {\r\n    D d; \/\/ No error\r\n}\r\n<\/pre>\n<pre>\r\n          A\r\nvirtual \/   \\ virtual\r\n      B       C\r\n        \\   \/\r\n          D\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You need virtual inheritance to solve the diamond problem with multiple inheritance. Not working version A \/ \\ B C<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false},"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19310"}],"collection":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19310"}],"version-history":[{"count":10,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19310\/revisions"}],"predecessor-version":[{"id":19320,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/19310\/revisions\/19320"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19310"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}