{"id":322,"date":"2009-08-01T21:28:02","date_gmt":"2009-08-01T20:28:02","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=322"},"modified":"2009-08-03T11:31:11","modified_gmt":"2009-08-03T10:31:11","slug":"dynamic-casting","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2009\/08\/01\/dynamic-casting\/","title":{"rendered":"Dynamic Casting"},"content":{"rendered":"<p>Dynamic casting, means that you can convert one object into another that is off the same type.  For example, if you had a base class called Shape and a inherited class called Rectangle then you are able to convert a Shape object into a Rectangle.<\/p>\n<pre lang=\"cpp\">\r\nRectangle *rec = dynamic_cast<Shape *>(shapeobject);\r\n<\/pre>\n<p>sort of thing, there has to be a virtual function within the base class otherwise the compiler will complain, but apart from that that is about it.<\/p>\n<p>Dynamic casting allows for NULL returns which is the best thing, because you can test to see if the casting actually worked and not to do anything silly on a NULL object which would crash the program.<\/p>\n<p>Pointer casting uses the sytnax<\/p>\n<pre lang=\"cpp\">\r\n<type> *p_subclass = dynamic_cast<<type> *>( p_obj );\r\n<\/pre>\n<p>Reference will not throw an error\/expcetion so will need to check std::bad_cast< typeinfo header >, here is the syntax<\/p>\n<pre lang=\"cpp\">\r\n<type> subclass = dynamic_cast<<type> &>( ref_obj );\r\n<\/pre>\n<p>Hopefully this will make more sense for how and why it works.<\/p>\n<pre lang=\"cpp\">\r\n#include <iostream>\r\n\r\nusing namespace std;\r\n\r\nclass classA \r\n{\r\n\tpublic\t:\r\n\t\tint x;\t\t\/\/ should be private\r\n\t\tchar y;\t\t\/\/ should be private\r\n\r\n\t\tclassA();\r\n\t\t\t\r\n \t\tvirtual ~classA() {}; \t\/\/ need to have a virtual function for dynamic casting\r\n};\r\n\r\n\/\/ basic classA constructor\r\nclassA::classA()\r\n{ \r\n\tcout << \"classA\" << endl; \r\n\tx = 1; \r\n\ty = 'a';\r\n}\r\n\r\n\r\nclass classB : public classA\r\n{\t\r\n\tpublic :\r\n\t\tint b;\t\t\/\/ should be private\r\n\r\n\t\tclassB();\r\n\r\n\t\t~classB() {};\t\/\/ complete the virtual\r\n};\r\n\r\n\/\/ basic classB constructor\r\nclassB::classB()\r\n{\r\n\tcout << \"classB\" << endl;\r\n\tx = 2;\r\n\ty = 'b';\r\n}\r\n\r\nint main()\r\n{\r\n\tclassA newa;\t\/\/ classA obj\r\n\tcout << \"class A constructed\" << endl;\r\n\tclassB newb;\t\/\/ classB obj\r\n\tcout << \"class B constructed\" << endl;\r\n\r\n\tcout << \"NewA X \" << newa.x << endl;\r\n\tcout << \"NewB X \" << newb.x << endl;\r\n\r\n\t\/\/ point a classB to a already created classB object\r\n\tclassB *normalB = &newb;\r\n\t\/\/ dynamic_cast a normalB object (newb) to another classA object\r\n\tclassA *dynA = dynamic_cast<classB *>(normalB);\t\r\n\tcout << \"dynamic A X \" << dynA->x << endl;\r\n\r\n\t\/\/ does not work, because you cannot convert classA into a classB, but if classA was pointing to a classB type.\r\n\tclassA *normalA = &newa;\r\n\tclassB *dynB = dynamic_cast<classB *>(normalA);\r\n\tif (dynB)\t\/\/ above produces a 0 because invalid.\r\n\t{\r\n\t\tcout << \"dynamic B X \" << dynB->x << endl;\r\n\t\tcout << \"dynamic B X \" << dynB->b << endl;\r\n\t}\r\n\r\n\t\/\/ this does work because it is converting from a pointer of classB type, which was \"sitting\" in a classA container\r\n\t\/\/ and then is converted back to a classB\r\n\tclassA *normalA2 = &newb;\r\n\tclassB *dynB2 = dynamic_cast<classB *>(normalA2);\r\n\tif (dynB2)\t\/\/ above produces a 0 because invalid.\r\n\t{\r\n\t\tcout << \"dynamic A2X \" << normalA2->x << endl;\t\/\/ output is 2 because it was a classB constructed\r\n\t\tcout << \"dynamic B X \" << dynB2->x << endl;\r\n\t\tcout << \"dynamic B X \" << dynB2->b << endl;\r\n\t}\r\n\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Dynamic casting, means that you can convert one object into another that is off the same type. For example, if you had a base class called Shape and a inherited class called Rectangle then you are able to convert a Shape object into a Rectangle. Rectangle *rec = dynamic_cast(shapeobject); sort of thing, there has to &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2009\/08\/01\/dynamic-casting\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Dynamic Casting<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[10,8,398,400],"class_list":["post-322","post","type-post","status-publish","format-standard","hentry","category-c_and_cpp","tag-c","tag-gnu","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=322"}],"version-history":[{"count":4,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/322\/revisions"}],"predecessor-version":[{"id":325,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/322\/revisions\/325"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}