tag:blogger.com,1999:blog-80311103455741034932024-03-13T17:02:00.882-07:00Learn C++Pranit Kotharihttp://www.blogger.com/profile/07243667541835995174noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-8031110345574103493.post-22401441661660897402014-08-09T05:21:00.001-07:002014-08-09T05:21:54.072-07:00Leslie Lamport: Thinking Above the Code<iframe allowfullscreen="" frameborder="0" height="270" src="//www.youtube.com/embed/-4Yp3j_jk8Q" width="480"></iframe>Pranit Kotharihttp://www.blogger.com/profile/07243667541835995174noreply@blogger.com1tag:blogger.com,1999:blog-8031110345574103493.post-12461830130456983322014-04-27T06:10:00.000-07:002014-04-27T08:39:01.934-07:00C++ 11 tuple<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">In C++ 11 standard library, tuple is introduced which can
hold values of heterogeneous data type in one object. It is also known as
extended pair. We can keep any number of values in tuple. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Let’s see following example to understand how to use tuple,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">#include</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><tuple></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">#include</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><iostream></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">#include</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><string></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">using</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> std;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">
main(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argc</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">char</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">* </span><span style="background: white; color: grey; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argv</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">[])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">double</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">> info(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"Pranit"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,27,25000,86);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.youtube.com/embed/M6SkquQajIk?feature=player_embedded' frameborder='0'></iframe></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">To use tuple we first need to include tuple (</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">#include</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><tuple></span><span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">) header file
in C++ code. We will be using <i>cout</i>
and <i>string</i> so we also included
<string> and <iostream> header files. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">In code, we have initialized tuple <i>info</i> to keep information regarding ‘name’:string, ‘age’:int,
‘salary’:double, ‘year’:int. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal">
<span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">double</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">> info(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"Pranit"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">,27,25000,86);</span><span style="font-family: Consolas; font-size: 12pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Here we have defined and initialized tuple at same place, we
can also use convenience function to initialize value after defining it like,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">double</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">> info; </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//defining tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">info = make_tuple(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"Pranit"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">,27,25000,86); </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//initializing tuple</span><span style="color: green; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">To access values of tuple we can use <i>get</i> function,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">get<0>(info); </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// accessing name of info</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">get<2>(info); </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// accessing salary of info</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">get<3>(info); </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// accessing name of year</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">If by mistake you give out of range value to get function, it
will give compile time error. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">get<5>(info); </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//error :: index from 0 to 3 is only permissible</span><span style="color: green; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><i>get </i>function returns reference to element, so we can change
value of element by assigning value.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">get<2>(info) = 20000; </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// change value of salary from 250000 to 20000</span><span style="color: green; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">We can extract all values of tuple using std::tie function,
also, if we want to ignore any of the value we can use std::ignore.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">tie(name,age,salary,year) = info; </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// extracted all values in corresponding
variables </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">tie(name,std::ignore,salary,year) = info; </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// ignoring age value</span><span style="color: green; font-family: Consolas; font-size: 12.0pt; line-height: 115%; mso-bidi-font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Value of one tuple can be assigned to another tuple using
assignment operator.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">double</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">> info1;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt; line-height: 115%;">info1 = info;</span><span style="font-family: Consolas; font-size: 12pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Complete code,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">#include</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><tuple></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">#include</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><iostream></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">#include</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><string></span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">using</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">namespace</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> std;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">
main(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argc</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">char</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">* </span><span style="background: white; color: grey; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argv</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">[])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">double</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">> info(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"Pranit"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,27,25000,86);</span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//defining and initilizing</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//tuple<string,int,double,int>
info; //defining tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//info = make_tuple("Pranit",27,25000,86);
//initilizing tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> get<0>(info);
</span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// accessing name of info</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> get<2>(info);
</span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// accessing salary of info</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> get<3>(info);
</span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">// accessing name of year</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//get<5>(info); //error
:: index from 0 to 3 is only permissible </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> get<2>(info)
= 20000; </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//
change value of salary from 250000 to
20000</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> name;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> age;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">double</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> salary;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> year;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> tie(name,age,salary,year)
= info; </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//
extracted all values in corresponding variables </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> tie(name,std::ignore,salary,year)
= info; </span><span style="background: white; color: green; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">//
ignoring age value, other values are extracted</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">tuple</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"><</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">string</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">double</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">,</span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">> info1;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> info1 =
info;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 12.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 12pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
<br /></div>
</div>
Pranit Kotharihttp://www.blogger.com/profile/07243667541835995174noreply@blogger.com1tag:blogger.com,1999:blog-8031110345574103493.post-73633214061471378002014-04-26T08:24:00.004-07:002014-04-26T08:28:18.482-07:00Calling Conventions <div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Calling conventions is very important concept of C++
programming. Calling conventions decides two things, how arguments will be
passed on stack and who will clear the stack. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen='allowfullscreen' webkitallowfullscreen='webkitallowfullscreen' mozallowfullscreen='mozallowfullscreen' width='320' height='266' src='https://www.youtube.com/embed/GhNG22Iryo8?feature=player_embedded' frameborder='0'></iframe></div>
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span>
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">In this article I will describe (and will prove it) two calling
conventions, __cdecl and __stdcall.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">In case of both __cdecl and __stdcall, arguments will be
pushed on stack from right to left. Difference is, in __cdecl caller clears the
stack, while in __stdcall callee will clear stack. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Let’s consider following code,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">__stdcall</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> StdCall(</span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">a</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">,</span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">b</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">a</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> + </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">b</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">__cdecl</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> Cdecl(</span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">a</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">,</span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">b</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">a</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> + </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">b</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: #6f008a; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">main</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">(</span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argc</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">, </span><span style="background: white; color: #2b91af; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">char</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">* </span><span style="background: white; color: grey; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argv</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">[])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> StdCall(10,20);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> Cdecl(10,20);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> </span><span style="background: white; color: blue; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; line-height: 115%;">}</span><span style="font-family: Consolas; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<br />
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">In this code, StdCall and Cdecl take two ints and returns
there addition. I kept code simple to illustrate concept. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Now we will see code’s assembly equivalent to understand how
calling conventions do the trick. I am
using Visual Studio to get assembly code. Here I am showing assembly code in
parts.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Call to StdCall<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">push 20 ;
00000014H<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">push 10 ;
0000000aH<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: #558ed5; font-family: Consolas; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">call ?StdCall@@YGHHH@Z ; StdCall</span><span style="color: #558ed5; font-family: Consolas; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"> Definition of StdCall <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; font-size: 10.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;">?StdCall@@YGHHH@Z
PROC ;
StdCall, COMDAT<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> push ebp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> mov ebp,
esp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> sub esp,
192 ; 000000c0H<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> push ebx<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> push esi<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> push edi<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> lea edi,
DWORD PTR [ebp-192]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> mov ecx,
48 ;
00000030H<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> mov eax,
-858993460 ;
ccccccccH<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> rep stosd<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> mov eax,
DWORD PTR _a$[ebp]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> add eax,
DWORD PTR _b$[ebp]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> pop edi<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> pop esi<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> pop ebx<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> mov esp,
ebp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> pop ebp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;"> ret 8<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;">?StdCall@@YGHHH@Z
ENDP ;
StdCall<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;">_TEXT ENDS<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #558ed5; font-family: Consolas;">END<span style="font-size: x-small;"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">In call to StdCall, it pushes arguments from right to left,
i.e. 20, 10. And look at definition of StdCall, you can see <i>ret 8</i>. This is return statement, and don’t
get confused with return statement of C++, here return statement doesn’t return
value, but pass control from where this function getting called. <i>ret 8</i> means it will return control by
clearing stack with 8 bytes (adds 8 bytes to stack pointer, esp ). In 32 bit OS
int are of 4 bytes, we are passing two ints so that will make it 8 bytes. So
callee has cleaned the stack. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Now look at Cdecl calling convention. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Call to Cdecl<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">push 20 ;
00000014H<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">push 10 ;
0000000aH<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">call ?Cdecl@@YAHHH@Z ; Cdecl<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">add esp, 8<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Definition of Cdecl <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">?Cdecl@@YAHHH@Z
PROC ; Cdecl,
COMDAT<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> push ebp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> mov ebp,
esp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> sub esp,
192 ; 000000c0H<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> push ebx<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> push esi<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> push edi<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> lea edi,
DWORD PTR [ebp-192]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> mov ecx,
48 ; 00000030H<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> mov eax,
-858993460 ; ccccccccH<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> rep stosd<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> mov eax,
DWORD PTR _a$[ebp]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> add eax,
DWORD PTR _b$[ebp]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> pop edi<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> pop esi<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> pop ebx<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> mov esp,
ebp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> pop ebp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"> ret 0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: #558ed5; font-family: Consolas; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">?Cdecl@@YAHHH@Z
ENDP ; Cdecl<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: #558ed5; font-family: Consolas; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;">_TEXT ENDS</span><span style="font-size: 16.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background: white; color: #558ed5; font-family: Consolas; line-height: 115%; mso-bidi-font-size: 9.5pt; mso-highlight: white; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #558ED5; mso-style-textfill-fill-colortransforms: "lumm=60000 lumo=40000"; mso-style-textfill-fill-themecolor: text2; mso-themecolor: text2; mso-themetint: 153;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Like StdCall, here in Cdecl both the arguments are pushed
from right to left, and then call to function is made. However after call statement,
you can see <i>add esp,
8, </i>which adds 8 to esp. esp points to top of stack. Adding 8 to means it is
clearing 8 bytes (because stack grows from higher to lower memory). So here
caller is clearing stack. Contrary to StdCall here you can see, <i>ret 0</i>, which just returns control and do
not anything to stack pointer. So, in this case caller clears the stack. <o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Please add comments for your queries and suggestions. </span></div>
</div>
Pranit Kotharihttp://www.blogger.com/profile/07243667541835995174noreply@blogger.com1tag:blogger.com,1999:blog-8031110345574103493.post-26024163795407332172014-04-03T10:13:00.001-07:002014-04-05T11:03:16.105-07:00Never throw exception from destructor<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">In C++, if you get exception in your code, you can handle it
using <i>try … catch </i>block. If your code in <i>try </i>block throws exception, it will be
caught in <i>catch </i>block. </span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">In catch block
you can either gracefully close, or resume your application depending on
severity of exception. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">If you do not handle exception in <i>try.. catch</i> block, C++
runtime will call <i>abort()</i> function and application will <b>crash</b>. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">So code which can throw exception should be wrapped in <i>try ..
catch</i> block. So long so good. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Compiler is good at handling one
exception at a time. If your code try to throw more than one exception at a time,
application will definitely crash by calling abort() function. (I don’t know why)
<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">And for the same reason, it is considered to be <i>bad practice</i>,
to throw exception from <i>destructor</i>. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Let’s see some examples to understand concept.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Here, I am throwing exception from try and it will get caught
in catch. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">
main(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argc</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">char</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">* </span><span style="background: white; color: grey; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argv</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">[])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">try</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> { <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">throw</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"Exc"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">catch</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">(...)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> std::cout<<</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"Exception
caught"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"><<std::endl;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><u>Output</u>: Exception caught <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Now, consider following example, I am throwing exception from
destructor. Destructor will get called
in two cases, 1) You have created object on heap using new operator and, call delete
to destroy object. 2) You have created object on stack, and object goes out of
scope. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">class</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">Sample</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">public</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> ~Sample()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">throw</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"Exc"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">};<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">
main(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argc</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">, </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">char</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">* </span><span style="background: white; color: grey; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">argv</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">[])<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">try</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> { <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">Sample</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> s;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">throw</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"ExcMain"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> <span style="color: #b6d7a8;">//other
code</span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">catch</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;">(...)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> std::cout<<</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">"Exception
caught"</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"><<std::endl;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 14.0pt; mso-bidi-font-size: 9.5pt; mso-highlight: white;">return</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt; line-height: 115%;">}</span><span style="font-family: Consolas; font-size: 14pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 14pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><u>Output </u>: <span style="color: red;">Crash!!!</span></span><span style="font-family: Consolas; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">When “throw ExcMain”, <i>throws </i>exception, <i>stack unwinding</i>
starts, and your object “s” goes out of scope,
which calls it’s destructor, which will <i>again </i>throw exception. </span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">So you
have two exceptions at a time. And C++ runtime will call <i>abort</i>().<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">So never throw exception from destructor. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Please give your suggestion and feedback by leaving comment
below article. <o:p></o:p></span></div>
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><br /></span></div>
<br />
<div class="MsoNormal">
<span style="font-size: 14.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;">Thanks.</span><span style="font-size: 18.0pt; line-height: 115%; mso-bidi-font-size: 11.0pt;"><o:p></o:p></span></div>
</div>
Pranit Kotharihttp://www.blogger.com/profile/07243667541835995174noreply@blogger.com3tag:blogger.com,1999:blog-8031110345574103493.post-88890408394669604042013-10-31T00:17:00.003-07:002013-10-31T01:29:36.266-07:00auto variable in C++ 11<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
C++ 11 standard made lot many improvements to C++. New
features like rvalue reference, lambda functions, auto variable and many more
added to C++. There was auto variable in previous C++ also, but that has
completely new meaning now. C++ 11 standard committee members, which also includes
big organizations like, Microsoft, Apple, Google, IBM, decided C++98 auto variable
is pretty useless, and new meaning can be given to auto keyword with little
impact.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
In this article we will discuss about auto variables. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Let’s start simple first,</div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> i = 10;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">auto</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> a = 10;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> cout<<</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-bidi-font-family: Consolas; mso-highlight: white;">typeid</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">(i).name() << </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-bidi-font-family: Consolas; mso-highlight: white;">" "</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><<</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-bidi-font-family: Consolas; mso-highlight: white;">typeid</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;">(a).name()<<endl;</span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-bidi-font-family: Consolas; mso-highlight: white;">// int int</span></div>
<div class="MsoNormal">
<span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-bidi-font-family: Consolas; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal">
We have explicitly mentioned ‘i’ as integer. For variable
‘a’ we left to compiler to decide its type.
As 10 is integer, type of a would be int. So <i>cout</i> will print<b> ‘int int’ </b>as
output. </div>
<div class="MsoNormal">
Let’s take bit complex example,</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">vector</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">int</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">> v;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> v.push_back(1);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> v.push_back(2);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> v.push_back(3);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> v.push_back(4);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> v.push_back(5);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">//print all elements of
vector -- old C++ way</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><b><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">vector</span></b><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><</span></b><b><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">int</span></b><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">>::</span></b><b><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">iterator</span></b><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> it = v.begin();</span></b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">it!= v.end(); it++)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> cout<<*it;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">//print all elements of
vector -- used C++ 11 auto </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">for</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;">(</span><b><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-bidi-font-family: Consolas; mso-highlight: white;">auto</span></b><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> it = v.begin();</span></b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> it != v.end(); it++)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt;"> cout<<*it;<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"> }</span><span style="font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; font-family: Consolas; font-size: 9.5pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
You can see how clumsy looking iterator variable definition
can be easily replaced by auto keyword. You need not to explicitly specify
which container you are using, and no need to remember what it contains, just <b>v.begin()</b> tells compiler its type. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
At very least it makes code look cleaner. </div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
One thing I wanted to bring to notice that C++ is statically
typed language, and compiler has to know data type of object at compile time.
So, data type of auto variables will also be decided at compile time only.</div>
<div class="MsoNormal">
There are <a href="http://www.blogger.com/blogger.g?blogID=8031110345574103493" name="_GoBack"></a>more concrete reasons to use
auto variable with lambda functions that I will cover in article for lambda
functions. </div>
<div class="MsoNormal">
<br /></div>
<br />
<div class="MsoNormal">
If you have any queries or suggestions, please put comments
below. </div>
</div>
Pranit Kotharihttp://www.blogger.com/profile/07243667541835995174noreply@blogger.com15tag:blogger.com,1999:blog-8031110345574103493.post-31826471922296270992013-08-24T05:15:00.000-07:002013-10-31T02:14:34.664-07:00C++ style casts<div dir="ltr" style="text-align: left;" trbidi="on">
<h1 style="color: #365f91; font-family: Cambria, serif; font-size: 14pt; line-height: 21px; margin: 24pt 0in 0.0001pt; page-break-after: avoid;">
C++ Style Casts</h1>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">C style casting can be abused in many ways. First without much effort one can change type of variable or expression, secondly it is hard to identify where type casting is used, because you just need parenthesis and identifier to do type casting. It is difficult to distinguish normal expression and type casting in C source code, so text finding tools will also be of little help.<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">C style cast cannot be completely blamed because it was good with C code; C++ has inheritance and polymorphism, which were foreign concepts for C, so there was no reason for C to distinguish between <span class="SpellE">upcasting</span> and <span class="SpellE">downcasting</span>. <o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">To overcome these issues C++ came up with four new casting keywords viz., static_cast, dynamic_cast, const_cast, reinterpret_cast.<o:p></o:p></span></div>
<h1 style="color: #365f91; font-family: Cambria, serif; font-size: 14pt; line-height: 21px; margin: 24pt 0in 0.0001pt 0.5in; page-break-after: avoid; text-indent: -0.25in;">
1.<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal;"> </span>static_cast</h1>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">This is general purpose cast, and generally used for casting like from <i>int to double</i>. So whenever you want to typecast, you most of time end up using static_cast. Let’s take following expression,<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">double</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"> b = <b>(</b></span><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">double</span></b><b><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">)</span></b><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">10/20; </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: green; font-family: Consolas; font-size: 12pt; line-height: 18px;">/*C style cast from int to double */</span><span style="color: green; font-family: Consolas; font-size: 12pt; line-height: 18px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">double</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"> b = </span><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">static_cast</span></b><b><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"><</span></b><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">double</span></b><b><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">></span></b><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"> (10) /20; </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: green; font-family: Consolas; font-size: 12pt; line-height: 18px;">// C++ style cast</span><span style="color: green; font-family: Consolas; font-size: 12pt; line-height: 18px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">Looks clumsy! Yes it is. This is blessing in disguised. It is not easy to read and write syntax, and so you can easily distinguish type casting from other code, secondly all <i>static_cast</i> can be easily found using ‘text search’ tools.<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">See following code to understand how to use static_cast in C++ program.<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">#include</span><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt; line-height: 18px;">"<span class="SpellE">stdafx.h</span>"</span><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">#include</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt;"><<span class="SpellE">iostream</span>></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">using</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">namespace</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> <span class="SpellE">std</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> main(</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="SpellE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: Consolas; font-size: 12pt;">argc</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">, </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">char</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">* </span><span class="SpellE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: Consolas; font-size: 12pt;">argv</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">[])<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> a = 10;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> b = 20;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">double</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> d = </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">static_cast</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">double</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">>(a)/b;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> <span class="SpellE"><span class="GramE">cout</span></span><<d;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">return</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt; text-align: justify; text-indent: 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">}</span><span style="font-family: Consolas; font-size: 12pt; line-height: 18px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt; text-align: justify; text-indent: 0.25in;">
<span style="font-family: Consolas; font-size: 12pt; line-height: 18px;">Output: 0.5<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt; text-align: justify; text-indent: 0.25in;">
<br /></div>
<h1 style="color: #365f91; font-family: Cambria, serif; font-size: 14pt; line-height: 21px; margin: 24pt 0in 0.0001pt 0.5in; page-break-after: avoid; text-indent: -0.25in;">
2.<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal;"> </span>const_cast</h1>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">Consider following code snippet<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt; text-indent: 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">const</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> a = 10;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">int</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"> *b = </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">static_cast</span><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"><</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">int*</span><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">>(&a);</span><span style="font-family: Consolas; font-size: 12pt; line-height: 18px;"> </span><span style="color: #00b050; font-family: Consolas; font-size: 12pt; line-height: 18px;">//error static_cast cannot takeaway <span class="SpellE">constness</span><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">Problem with above code you are trying to take away const-ness using static_cast and C++ won’t allow this. For this there is special keyword and that is const_cast. <span class="SpellE">Rewritting</span> above code as,<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt; text-indent: 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">const</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> a = 10;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">int</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"> *b = </span><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">const_cast</span></b><b><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;"><</span></b><b><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt; line-height: 18px;">int</span></b><b><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">*></span></b><span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">(&a);</span><span style="color: #00b050; font-family: Consolas; font-size: 12pt; line-height: 18px;">//works fine</span><span style="font-family: Consolas; font-size: 12pt; line-height: 18px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">Compiler will accept this happily. So, if you want to take away const or volatile use const_cast and not any other cast.<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">So there is one more advantage of using C++ style cast, that you need to consciously decide and specify what you intent to do. In C there is no such distinction.<o:p></o:p></span></div>
<h1 style="color: #365f91; font-family: Cambria, serif; font-size: 14pt; line-height: 21px; margin: 24pt 0in 0.0001pt 0.5in; page-break-after: avoid; text-indent: -0.25in;">
3.<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal;"> </span>dynamic_cast</h1>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt;">
<br /></div>
<div class="MsoListParagraph" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span class="SpellE"><span style="font-size: 12pt; line-height: 18px;">Upcasting</span></span><span style="font-size: 12pt; line-height: 18px;"> in is perfectly acceptable, since it is casting to generalized type in hierarchy. While doing <span class="SpellE">downcasting</span> may not be always correct. <span class="GramE">dynamic_cast</span> prevents form incorrect <span class="SpellE">downcasting</span>. If casting is valid result of dynamic cast is valid pointer, otherwise it returns null. Consider following code,<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">#include</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt;"><<span class="SpellE">iostream</span>></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">using</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">namespace</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> <span class="SpellE">std</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">class</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">shape</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">{</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">public</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> :</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">virtual</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> ~shape(){}};<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">class</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="SpellE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">circle</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">:</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">public</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">shape</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">{};<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">class</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="SpellE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">square</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">:</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">public</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">shape</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">{};<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> main(</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="SpellE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: Consolas; font-size: 12pt;">argc</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">, </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">char</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">* </span><span class="SpellE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: Consolas; font-size: 12pt;">argv</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">[])<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">shape</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> *b = </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">new</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">circle</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">; </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: green; font-family: Consolas; font-size: 12pt;">//<span class="SpellE">Upcasting</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">square</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">* s = </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">dynamic_cast</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">square</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">*> (b);</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: green; font-family: Consolas; font-size: 12pt;">//<span class="SpellE">downcasting</span> to incorrect type</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> (<span class="GramE">s</span>!=</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #6f008a; font-family: Consolas; font-size: 12pt;">NULL</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">)?<span class="SpellE">cout</span><<</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt;">"Right cast"<<<span class="SpellE">endl</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">:<span class="SpellE">cout</span><<</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt;">"Wrong cast"<<<span class="SpellE">endl</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">circle</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> *c = </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">dynamic_cast</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #2b91af; font-family: Consolas; font-size: 12pt;">circle</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">*> (b);</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: green; font-family: Consolas; font-size: 12pt;">//<span class="SpellE">downcasting</span> to correct type</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> (<span class="GramE">c</span>!=</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #6f008a; font-family: Consolas; font-size: 12pt;">NULL</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">)?<span class="SpellE">cout</span><<</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt;">"Right cast"<<<span class="SpellE">endl</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">:<span class="SpellE">cout</span><<</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt;">"Wrong cast"<<<span class="SpellE">endl</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">return</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt; text-align: justify; text-indent: 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">}</span><span style="font-size: 12pt; line-height: 18px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span class="GramE"><span style="font-size: 12pt; line-height: 18px;">Output :</span></span><span style="font-size: 12pt; line-height: 18px;"> Wrong Cast<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;"> Right Cast<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">In program, first we have <span class="SpellE">upcasted</span> circle pointer to shape which is perfectly acceptable, then we are trying to downcast <span class="SpellE">upcasted</span> circle pointer to square pointer using dynamic_cast which is not acceptable as it is incorrect conversion. <span class="GramE">Finally we downcast <span class="SpellE">upcasted</span> circle pointer to circle pointer using dynamic_cast, which is perfectly acceptable.</span><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">Remember, with old style cast both the casting (from circle to circle and from square to circle) is permissible, and program is leaved to its fate.<o:p></o:p></span></div>
<h1 style="color: #365f91; font-family: Cambria, serif; font-size: 14pt; line-height: 21px; margin: 24pt 0in 0.0001pt 0.5in; page-break-after: avoid; text-indent: -0.25in;">
4.<span style="font-family: 'Times New Roman'; font-size: 7pt; font-weight: normal; line-height: normal;"> </span>reinterpret_cast</h1>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">This is unsafe. It can convert int to pointer and any class pointer to any class pointer. Hope you understand ;). When you convert from any unrelated type to any unrelated type, no one guarantees expected output. <span class="GramE">Unpredictable behavior.</span> For sake of completeness of article, with advice of not to use it, see following example of reinterpret_cast.<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">#include</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt;"><<span class="SpellE">iostream</span>></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">using</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">namespace</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> <span class="SpellE">std</span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<br /></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> main(</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="SpellE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: Consolas; font-size: 12pt;">argc</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">, </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">char</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">* </span><span class="SpellE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: grey; font-family: Consolas; font-size: 12pt;">argv</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">[])<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">int</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> a = 0x1233254;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">char</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> *<span class="SpellE">ptr</span> = </span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">reinterpret_cast</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"><</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">char</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">*> (a);<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> *<span class="SpellE">ptr</span> = 3;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> <span class="SpellE"><span class="GramE">cout</span></span><<</span><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: #a31515; font-family: Consolas; font-size: 12pt;">"Enjoy!!"</span><span style="background-color: white; font-family: Consolas; font-size: 12pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; margin: 0in 0in 0.0001pt 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt;"> </span><span class="GramE"><span style="background-color: white; background-position: initial initial; background-repeat: initial initial; color: blue; font-family: Consolas; font-size: 12pt;">return</span></span><span style="background-color: white; font-family: Consolas; font-size: 12pt;"> 0;<o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt; text-align: justify; text-indent: 0.25in;">
<span style="background-color: white; font-family: Consolas; font-size: 12pt; line-height: 18px;">}</span><span style="font-family: Consolas; font-size: 12pt; line-height: 18px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">So, we have covered all types of C++ style cast.</span><span style="font-family: Consolas; font-size: 12pt; line-height: 18px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
Please add comments below, about your suggestions and views on this.</div>
<div class="MsoNormal" style="font-family: Calibri, sans-serif; font-size: 11pt; line-height: 17px; margin: 0in 0in 10pt 0.25in; text-align: justify;">
<span style="font-size: 12pt; line-height: 18px;">Happy programming!!</span></div>
</div>
Pranit Kotharihttp://www.blogger.com/profile/07243667541835995174noreply@blogger.com11