{"id":1041,"date":"2018-02-22T16:03:38","date_gmt":"2018-02-22T16:03:38","guid":{"rendered":"http:\/\/www.max-sperling.bplaced.net\/?p=1041"},"modified":"2024-02-16T10:26:58","modified_gmt":"2024-02-16T10:26:58","slug":"when-to-use-which-cast-c","status":"publish","type":"post","link":"http:\/\/www.max-sperling.bplaced.net\/?p=1041","title":{"rendered":"When to use which cast (C++)"},"content":{"rendered":"<p><strong>Types<\/strong><\/p>\n<table>\n<tr>\n<th>Type<\/th>\n<th>Area<\/th>\n<th>Risk<\/th>\n<\/tr>\n<tr>\n<td>static_cast<\/td>\n<td>&#8211;  Converts fundamental types<br \/>&#8211; Non-const to const<br \/>&#8211; Down-casts (dangerous)<\/td>\n<td>Medium<\/td>\n<\/tr>\n<tr>\n<td>dynamic_cast<\/td>\n<td>&#8211; Down- and side-casts<\/td>\n<td>Low<\/td>\n<\/tr>\n<tr>\n<td>const_cast<\/td>\n<td>&#8211; Removes and adds const<\/td>\n<td>High<\/td>\n<\/tr>\n<tr>\n<td>reinterpret_cast<\/td>\n<td>&#8211; Converts nearly everthing<br \/>&#8211; Not: const to non-const<\/td>\n<td>Very high<\/td>\n<\/tr>\n<\/table>\n<p>C-style casts in C++ are trying the following casts in the following order:<\/p>\n<ol>\n<li>const_cast<\/li>\n<li>static_cast<\/li>\n<li>static_cast, then const_cast<\/li>\n<li>reinterpret_cast<\/li>\n<li>reinterpret_cast, then const_cast<\/li>\n<\/ol>\n<hr>\n<p><strong>Usage<\/strong><\/p>\n<p>Good projects should be designed that it&#8217;s not often necessary to cast types (explicit conversion).<\/p>\n<p>Those few casts should be just static_cast and dynamic_cast. Use static_cast for conversions between fundamental types and dynamic_cast for down- and side-casts. If the static_cast is not possible you got an error during compile-time and if the dynamic_cast is failing you got for pointer a nullptr and for references an bad_cast exception during run-time.<\/p>\n<p>Don&#8217;t use const_cast, reinterpret_cast and C-style cast. If something has been defined const its for a reason, so don&#8217;t remove it with a const_cast. Think about chages in your class structures if you really have to use reinterpret_cast. But the worst are C-style casts, they are just evil!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Types Type Area Risk static_cast &#8211; Converts fundamental types&#8211; Non-const to const&#8211; Down-casts (dangerous) Medium dynamic_cast &#8211; Down- and side-casts<\/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\/1041"}],"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=1041"}],"version-history":[{"count":1,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1041\/revisions"}],"predecessor-version":[{"id":16761,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=\/wp\/v2\/posts\/1041\/revisions\/16761"}],"wp:attachment":[{"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1041"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1041"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.max-sperling.bplaced.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1041"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}