{"id":4129,"date":"2014-03-30T07:58:46","date_gmt":"2014-03-30T07:58:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-get-the-exact-type-of-a-pointer-in-run-time-for-virtual-functions-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:58:46","modified_gmt":"2014-03-30T07:58:46","slug":"how-to-get-the-exact-type-of-a-pointer-in-run-time-for-virtual-functions-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-get-the-exact-type-of-a-pointer-in-run-time-for-virtual-functions-collection-of-common-programming-errors\/","title":{"rendered":"How to get the exact type of a pointer in run time for virtual functions?-Collection of common programming errors"},"content":{"rendered":"<p>You can do this by using <code>dynamic_cast<\/code>, or the typeid operator. However, generally this is a very bad idea. If your virtual function needs to know the exact type of the object, then something is wrong with your design.<\/p>\n<p>If you have this situation:<\/p>\n<pre><code>class treeNode\n{\n  virtual int foo()\n  {\n    \/\/ do some default thing, maybe nothing\n  }\n};\n\nclass splitNode : public treeNode\n{\n  virtual int foo()\n  {\n    \/\/ do whatever splitNode wants to do\n  }\n};\n\nclass leafNode : public treeNode\n{\n  virtual int foo()\n  {\n    \/\/ do whatever leafNode wants to do\n  }\n};\n<\/code><\/pre>\n<p>and you have a pointer <code>treeNode *p;<\/code> that points to either a <code>splitNode<\/code> or a <code>leafNode<\/code> object, and you call <code>p-&gt;foo()<\/code>, then the appropriate version of <code>foo()<\/code> will be called. That is the whole point of virtual functions.<\/p>\n<p>Also, see this question.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can do this by using dynamic_cast, or the typeid operator. However, generally this is a very bad idea. If your virtual function needs to know the exact type of the object, then something is wrong with your design. If you have this situation: class treeNode { virtual int foo() { \/\/ do some default [&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-4129","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4129","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=4129"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4129\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}