{"id":4285,"date":"2014-03-30T09:42:16","date_gmt":"2014-03-30T09:42:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-it-safe-to-use-static_cast-in-this-polymorphic-scenario-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:42:16","modified_gmt":"2014-03-30T09:42:16","slug":"is-it-safe-to-use-static_cast-in-this-polymorphic-scenario-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-it-safe-to-use-static_cast-in-this-polymorphic-scenario-collection-of-common-programming-errors\/","title":{"rendered":"Is it safe to use static_cast in this polymorphic scenario?-Collection of common programming errors"},"content":{"rendered":"<p>I have a type heirarchy:<\/p>\n<pre><code>class Object {\n...\n};\n\nclass Node : public Object {\n...\n};\n\nclass Leaf : public Object {\n...\n};\n\nclass Something : public Node {\n...\n};\n\nclass SomethingElse : public Leaf {\n...\n};\n<\/code><\/pre>\n<p>In other words, absolutely every class inherits <code>Object<\/code> either directly or indirectly.<\/p>\n<p>The constructor for every object is in the following format:<\/p>\n<pre><code>ClassType(Object * parent)\n<\/code><\/pre>\n<p>However, by design only a <code>Node<\/code> can be a parent, while a <code>Leaf<\/code> is a terminating node;<\/p>\n<p>In the moment, in every constructor I am doing the following:<\/p>\n<pre><code>        Node * p = dynamic_cast(parent);\n        if (p) p-&gt;adopt(this);\n        else qDebug()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have a type heirarchy: class Object { &#8230; }; class Node : public Object { &#8230; }; class Leaf : public Object { &#8230; }; class Something : public Node { &#8230; }; class SomethingElse : public Leaf { &#8230; }; In other words, absolutely every class inherits Object either directly or indirectly. The [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4285","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4285"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4285\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}