{"id":277,"date":"2010-06-23T09:10:26","date_gmt":"2010-06-23T09:10:26","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=277"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"cast","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/memory\/cast","title":{"rendered":"Cast"},"content":{"rendered":"<h4>Cast &#8211; Converting a variable to a different type in an operation (int*)<\/h4>\n<p>Although necessary at times, casts are inherently dangerous (becuase you don&#8217;t get a compiler warning that a potentially bad conversion will occur).  It is more nomral to just allow C++ to do its normal automatic conversion which will typially involve increasing the size of everything to match the largest variable, and then downsizing if necessary to fit the result in the destination variable.  However there are times when you&#8217;ll want to specify a conversion.<\/p>\n<p>These are the explicit conversions available in C++:<\/p>\n<p>static_cast<\/p>\n<p style=\"padding-left: 30px;\">Use for any type conversion that we want to tell the compiler to do and to tell it that we know we\u00a0want it so there&#8217;s no need to warn us about it happening (this type of cast is considered OK to\u00a0use occasionally)<\/p>\n<p>dynamic_cast<\/p>\n<p style=\"padding-left: 30px;\">Supports the run time identification of objects addressed by a pointer of reference (this type of cast\u00a0is considered OK to use occasionally)<\/p>\n<p>const_cast<\/p>\n<p style=\"padding-left: 30px;\">Use to cast away const&#8217;ness (e.g. great when calling a function that only takes a variable but\u00a0you want to supply a constant).  (Using this almost always indicates a design flaw &#8211; it should\u00a0not be necessary)<\/p>\n<p>reinterpret_cast<\/p>\n<p style=\"padding-left: 30px;\">Performs a low level reintrepretation of the bit pattern of its operands &#8211; machine dependant.  (This\u00a0cast is considered hazardous for bugs &#8211; don&#8217;t use)<\/p>\n<p>The standard C cast, e.g. (int*) is equivalent to const_cast, static_cast or reinterpret_cast depending on the types involved.  C++ introduced these named casts to give a more finely tuned tool when casts are necessary.  The C casts are supports under C++ to provide backwards compatibility.<\/p>\n<p>Every time you use a cast you should think hard about wether you can acheive the same result a different way.  Not using them helps reduce bugs.<\/p>\n<p>Example usage:<\/p>\n<pre><code>\r\n\ti_val *= static_cast(d_dal);\t\t\/\/Converts d_val to int\r\n\r\n\tconst char *pc_str;\r\n\tchar *pc = string_copy(const_cast(pc_str);\t\/\/Converts d_val to int\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Cast &#8211; Converting a variable to a different type in an operation (int*) Although necessary at times, casts are inherently dangerous (becuase you don&#8217;t get a compiler warning that a potentially bad conversion will occur). It is more nomral to just allow C++ to do its normal automatic conversion which will typially involve increasing the [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-277","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/277\/revisions"}],"predecessor-version":[{"id":1112,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/277\/revisions\/1112"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=277"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=277"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}