{"id":918,"date":"2024-08-09T20:18:36","date_gmt":"2024-08-09T20:18:36","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=918"},"modified":"2025-01-25T21:08:16","modified_gmt":"2025-01-25T21:08:16","slug":"small-enhancements-over-c","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/beginnings\/small-enhancements-over-c\/","title":{"rendered":"Small Enhancements over C"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Overview_of_Small_Enhancements\"><\/span>Overview of Small Enhancements<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<ul class=\"wp-block-list has-medium-font-size\">\n<li>New keywords and reserved words<\/li>\n\n\n\n<li>Line-style comments<\/li>\n\n\n\n<li>Stronger typing<\/li>\n\n\n\n<li><strong>struct<\/strong>\/<strong>union<\/strong>\/<strong>enum<\/strong> tags become true type names<\/li>\n\n\n\n<li>Declaration placement<\/li>\n\n\n\n<li>Scope operator<\/li>\n\n\n\n<li><strong>const<\/strong> specifier (stricter than ANSI C)<\/li>\n\n\n\n<li>Anonymous <strong>union<\/strong><\/li>\n\n\n\n<li>Explicit (function-like) type conversion<\/li>\n\n\n\n<li>Function prototypes (stricter than ANSI C)<\/li>\n\n\n\n<li>Overloading of function names<\/li>\n\n\n\n<li>Default values for function parameters<\/li>\n\n\n\n<li>Functions with unspecified number of parameters (stricter than ANSI C)<\/li>\n\n\n\n<li>Reference parameters in functions<\/li>\n\n\n\n<li><strong>inline<\/strong> specifier<\/li>\n\n\n\n<li><strong>new<\/strong> and <strong>delete<\/strong> operators&nbsp;<\/li>\n\n\n\n<li>Pointers to <strong>void<\/strong> and functions returning <strong>void<\/strong> (stricter than ANSI C)<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"new-keywords\"><span class=\"ez-toc-section\" id=\"New_Keywords_and_Reserved_Words\"><\/span>New Keywords and Reserved Words<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>C keywords<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while <\/strong><\/code><\/pre>\n\n\n\n<p>C++ keywords added:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>and<\/strong><strong><em> <\/em><\/strong><strong>and_eq<\/strong><strong><em> <\/em><\/strong><strong>asm<\/strong><strong> <\/strong><strong>bitand<\/strong><strong> <\/strong><strong>bitor<\/strong><strong> <\/strong><strong>bool<\/strong><strong> <\/strong><strong>catch<\/strong><strong> <\/strong><strong>class<\/strong><strong> <\/strong><strong>compl<\/strong><strong> <\/strong><strong>const_cast<\/strong><strong> <\/strong><strong>delete<\/strong><strong> <\/strong><strong>dynamic_cast<\/strong><strong> <\/strong><strong>explicit<\/strong><strong> <\/strong><strong>export<\/strong><strong> <\/strong><strong>false<\/strong><strong> <\/strong><strong>friend<\/strong><strong> <\/strong><strong>if<\/strong><strong> <\/strong><strong>inline<\/strong><strong> <\/strong><strong>mutable<\/strong><strong> <\/strong><strong>namespace<\/strong><strong> <\/strong><strong>new<\/strong><strong> <\/strong><strong>not<\/strong><strong> <\/strong><strong>not_eq<\/strong><strong> <\/strong><strong>operator<\/strong><strong> <\/strong><strong>or<\/strong><strong> <\/strong><strong>or_eq<\/strong><strong> <\/strong><strong>private<\/strong><strong> <\/strong><strong>protected<\/strong><strong> <\/strong><strong>public<\/strong><strong> <\/strong><strong>reinterpret_cast<\/strong><strong> <\/strong><strong>static_cast<\/strong><strong> <\/strong><strong>template<\/strong><strong> <\/strong><strong>this<\/strong><strong> <\/strong><strong>throw<\/strong><strong> <\/strong><strong>true<\/strong><strong> <\/strong><strong>try<\/strong><strong> <\/strong><strong>typeid<\/strong><strong> <\/strong><strong>typename<\/strong><strong> <\/strong><strong>using<\/strong><strong> <\/strong><strong>virtual<\/strong><strong> <\/strong><strong>w_char_t<\/strong><strong> <\/strong><strong>xor<\/strong><strong> <\/strong><strong>xor_eq<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>bool<\/strong>, <strong>true<\/strong>, <strong>false<\/strong> are used for the C++ Boolean type<\/li>\n\n\n\n<li><strong>catch<\/strong>, <strong>throw<\/strong>, <strong>try<\/strong> are used for C++ exception handling<\/li>\n\n\n\n<li><strong>w_char_t<\/strong> in C is a <em>typedef<\/em>; in C++ it becomes a built-in type<\/li>\n\n\n\n<li><strong>and<\/strong>, <strong>and_eq<\/strong>, <strong>bitand<\/strong>, <strong>bitor<\/strong>, <strong>compl<\/strong>, <strong>not<\/strong>, <strong>not_eq<\/strong>, <strong>or<\/strong>, <strong>or_eq<\/strong>, <strong>xor<\/strong>, and <strong>xor_eq<\/strong> are alternatives to the standard operator symbols.&nbsp;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"line-style-comments\"><span class=\"ez-toc-section\" id=\"Line-Style_Comments\"><\/span>Line-Style Comments<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/* \n * The C++ version of a famous program \n *\/\n#include &lt;iostream&gt;\n\nint main()\n{\n\tstd::cout &lt;&lt; &quot;Hello World&quot; &lt;&lt; std::endl;\t\n\t                  \/\/ Output the famous message\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/* <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\"> * The C++ version of a famous program <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\"> *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Hello World&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t                  \/\/ Output the famous message<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li><strong>\/*<\/strong> and <strong>*\/<\/strong> are the standard C comment delimiters<\/li>\n\n\n\n<li><strong>\/\/<\/strong> starts a line comment; end of line ends the line comment<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"stronger-typing\"><span class=\"ez-toc-section\" id=\"Some_Differences_Due_to_Stronger_Typing\"><\/span>Some Differences Due to Stronger Typing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In C++, literal character constants (<strong>&#8216;a&#8217;<\/strong>) are of type <strong>char<\/strong>; in C, they are of type <strong>int<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In C, <strong>sizeof(&#8216;a&#8217;)<\/strong> equals <strong>sizeof(int)<\/strong><\/li>\n\n\n\n<li>In C++, <strong>sizeof(&#8216;a&#8217;)<\/strong> equals <strong>sizeof(char)<\/strong><\/li>\n<\/ul>\n\n\n\n<p>In C++, the use of an undeclared function is an error; in C, an undeclared function is assumed to be of type <strong>int foo()<\/strong> (K&amp;R-style):<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int main()\n{\n\tprintf(\"Hello world\\n\"); \/\/ Error in C++; printf not declared\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>In C++, assigning a <strong>void *<\/strong> pointer to any other type of pointer requires an explicit cast:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int main()\n{\n\tvoid  *vptr;\n\tfloat *fptr;\n     \/* ... *\/\n\tfptr = vptr;          \/\/ Error in C++!\n\tfptr = (float *)vptr; \/\/ OK\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p>In C++, the following:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>int mumble()<\/strong><\/code><\/pre>\n\n\n\n<p>declares <strong>mumble<\/strong> to be a function that has <em>zero<\/em> arguments.&nbsp; In C, it indicates that a function has an <em>unspecified number of arguments of unspecified type<\/em>.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In C++, the following initialization is illegal:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>char ch&#91;2] = \"Hi\";<\/strong><\/code><\/pre>\n\n\n\n<p>because it does not provide space for the terminating NUL character.<\/p>\n\n\n\n<p>The following is the recommended method:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>char ch&#91;] = \"Hi\";<\/strong><\/code><\/pre>\n\n\n\n<p>There is a <strong>bool<\/strong> type in C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>bool done = false;\t\/\/ bool to bool\nint  yesno = true;\t\/\/ bool to integer\nbool isalive = -100;\t\/\/ integer to bool\ndone = 1;\t\/\/ integer to bool\nisalive = ptr;\t\/\/ pointer to bool\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"structunionenum_Tags_Become_True_Type_Names\"><\/span>struct\/union\/enum Tags Become True Type Names<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;iostream&gt;\n\nenum account_type {SAVINGS,CHECKING};\nstruct bank_account\n{\n    int          id;\n    double       balance;\n    account_type type;\n};\n\nint main()\n{\n    bank_account checking\n   \t\t= {1234, 20000.00, CHECKING};\n\n    std::cout &lt;&lt; &quot;Balance for account # &quot;\n       &lt;&lt; checking.id &lt;&lt; &quot; type &quot;\n       &lt;&lt; checking.type &lt;&lt; &quot; is &quot;\n       &lt;&lt; checking.balance &lt;&lt; std::endl;\n    return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">enum<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">account_type<\/span><span style=\"color: #000000\"> {<\/span><span style=\"color: #0070C1\">SAVINGS<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #0070C1\">CHECKING<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">bank_account<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">          id;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\">       balance;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    account_type type;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    bank_account checking<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">   \t\t= {<\/span><span style=\"color: #098658\">1234<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">20000.00<\/span><span style=\"color: #000000\">, CHECKING};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Balance for account # &quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">       &lt;&lt; <\/span><span style=\"color: #001080\">checking<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">id<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; type &quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">       &lt;&lt; <\/span><span style=\"color: #001080\">checking<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">type<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; is &quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">       &lt;&lt; <\/span><span style=\"color: #001080\">checking<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">balance<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>You don&#8217;t have to precede the tag with a <strong>struct<\/strong>, etc.<\/li>\n\n\n\n<li><strong>cout<\/strong> and the <strong>&lt;&lt;<\/strong> operator can be &#8216;chained together&#8217; to replace the <strong>printf<\/strong> functionality.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Enumerations_are_Treated_More_Strictly\"><\/span>Enumerations are Treated More Strictly<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"enum Color {Red, Green, Blue};\nenum Fish {Herring, Mackerel, Salmon};\n\nint main()\n{\n\tColor hue;\n\tFish  swimmer;\n\tint c;\n\n\thue = Red;          \/\/ OK\n\tswimmer = Mackerel; \/\/ OK\n\thue = Salmon;       \/\/ Error!\n\tswimmer = Blue;     \/\/ Error!\n\tc = Green;          \/\/ OK!\n\thue = 1;            \/\/ Error!\n\thue = (Color) 1;    \/\/ OK\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">enum<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Color<\/span><span style=\"color: #000000\"> {<\/span><span style=\"color: #0070C1\">Red<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0070C1\">Green<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0070C1\">Blue<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">enum<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Fish<\/span><span style=\"color: #000000\"> {<\/span><span style=\"color: #0070C1\">Herring<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0070C1\">Mackerel<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0070C1\">Salmon<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tColor hue;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tFish  swimmer;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> c;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\thue = Red;<\/span><span style=\"color: #008000\">          \/\/ OK<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tswimmer = Mackerel;<\/span><span style=\"color: #008000\"> \/\/ OK<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\thue = Salmon;<\/span><span style=\"color: #008000\">       \/\/ Error!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tswimmer = Blue;<\/span><span style=\"color: #008000\">     \/\/ Error!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tc = Green;<\/span><span style=\"color: #008000\">          \/\/ OK!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\thue = <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #008000\">            \/\/ Error!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\thue = (Color) <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #008000\">    \/\/ OK<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>In C, <strong>enum<\/strong><strong>s<\/strong> are all of type <strong>int<\/strong>, and any enum is assignment compatible with an <strong>int<\/strong>.<\/li>\n\n\n\n<li>In C++, each <strong>enum<\/strong> is a <em>distinct<\/em> int type;&nbsp; an <strong>enum<\/strong> type can be assigned to an <strong>int<\/strong> type, <em>but not the other way around.<\/em><em>&nbsp;&nbsp;<\/em><\/li>\n\n\n\n<li>An <strong>enum<\/strong> is a subtype of an <strong>int<\/strong> &#8212; an enum <em>is a kind of <\/em><strong>int<\/strong>, but not vice versa.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Declaration_Placement\"><\/span>Declaration Placement<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;iostream&gt;\n\nint main()\n{\n\tfor (int i = 0; i &lt; 10; i++)\n\t\tstd::cout &lt;&lt; i &lt;&lt; '.';\n\n\t\/\/ ...\n\n\tchar *ptr = 0;\n\n\t\/\/ ...\n\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">for<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; i &lt; <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">; i++)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t\t<\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; i &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;.&#39;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *ptr = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Allows the declaration of a variable to be placed near its first use &#8212; can make the program more readable<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Scope_Operator\"><\/span>Scope Operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;iostream&gt;\n\nint value = 27;\n\nint main()\n{\n  double value = 16.298;\n\n  std::cout &lt;&lt; &quot;local value  = &quot; &lt;&lt; value\n     &lt;&lt; '\\n'\n     &lt;&lt; &quot;global value = &quot; &lt;&lt; ::value\n     &lt;&lt; std::endl;\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> value = <\/span><span style=\"color: #098658\">27<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> value = <\/span><span style=\"color: #098658\">16.298<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;local value  = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; value<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">     &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&#39;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">     &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;global value = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; ::value<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">     &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>The <em>scope operator<\/em>, (<strong>::<\/strong>), overrides the default scope (in this case, <em>local scope<\/em>) to <em>file scope<\/em><\/li>\n\n\n\n<li>The scope <em>cannot be overridden in the opposite direction<\/em><\/li>\n\n\n\n<li>The scope operator becomes indispensable with classes.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"const_Specifiers\"><\/span>const Specifiers<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p><strong>const<\/strong> means that a variable can&#8217;t be changed directly:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>const int stable = 1000;\nstable = 2000;\t\/\/ Error!\n<\/code><\/pre>\n\n\n\n<p>In C:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>const int arraysize = 100;\nint array&#91;arraysize];   <\/code><\/pre>\n\n\n\n<p>is illegal.&nbsp;<\/p>\n\n\n\n<p>In C++, it is legal &#8212; in C++, a <strong>const<\/strong> value may be used in place of any literal constant of its type.<\/p>\n\n\n\n<p>In C, the default scope of a <strong>const<\/strong> is <strong>extern<\/strong>;<br>In C++, the default scope of a <strong>const<\/strong> is <strong>static<\/strong>; much better for including in header files!<\/p>\n\n\n\n<p>Can localize the scope of a <strong>const<\/strong>, whereas a <strong>#define<\/strong> is effective from the point of its definition through the rest of the file(s) in the compilation.<\/p>\n\n\n\n<p>Often used in specifying function parameter types:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int blather(const char *readonly,\n\t\t    char *readwrite );<\/code><\/pre>\n\n\n\n<p>A <strong>const<\/strong> definition must be initialized:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>const char *fixed_ptr = somewhere;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Anonymous_unions\"><\/span>Anonymous unions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"struct token\n{\n    int id;\n    union\n    {\n        int   integer;\n        double real;\n    } value;\n\n   \t\/\/ ...\n\n} current_token;\n\t\n\t\/\/ ...\n\nf = current_token.value.real;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">token<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> id;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">union<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">   integer;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    } value;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">   \t\/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">} current_token;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">f = <\/span><span style=\"color: #001080\">current_token<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">value<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">real<\/span><span style=\"color: #000000\">;<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>In C, a union often introduces an annoying extra level of names that must be referenced:<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"struct token\n{\n    int id;\n    union\n    {\n        int   integer;\n        double real;\n    };\t\t\/\/ no name!\n    \n\t\t\/\/ ...\n\n} current_token;\n\n\t\/\/ ...\n\nf = current_token.real;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">token<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> id;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #0000FF\">union<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">   integer;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> real;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    };<\/span><span style=\"color: #008000\">\t\t\/\/ no name!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\t\/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">} current_token;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\/\/ ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">f = <\/span><span style=\"color: #001080\">current_token<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">real<\/span><span style=\"color: #000000\">;<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>In C++, an <em>anonymous union<\/em> avoids unnecessary names:<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Explicit_Type_Conversion\"><\/span>Explicit Type Conversion<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>C++ supports two kinds of syntax for a <em>cast<\/em>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The traditional C cast:&nbsp;<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int i = 5;\ndouble x = (double)i;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The new-style &#8216;function-like&#8217; cast:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int i = 5;\ndouble x = double(i);<\/code><\/pre>\n\n\n\n<p>The new form tends to be more natural when used with classes<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Function_Prototypes\"><\/span>Function Prototypes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Both C++ and ANSI C have function prototypes:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>extern FILE *fopen(const char *filename, \n                   const char *mode);\n      ...\nFILE *fp;\n      ...\nfp = fopen(\"myfile.dat\", \"r\");<\/code><\/pre>\n\n\n\n<p>In ANSI C, they are <em>optional<\/em>;&nbsp; the old K&amp;R style is still legal.<\/p>\n\n\n\n<p>In C++, <em>function prototypes are required<\/em>.<\/p>\n\n\n\n<p>In C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int foo();<\/code><\/pre>\n\n\n\n<p>is equivalent to:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int foo(void)<\/code><\/pre>\n\n\n\n<p><em>not the ANSI C (K&amp;R) interpretation !<\/em><\/p>\n\n\n\n<p>In C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int doit(...);<\/code><\/pre>\n\n\n\n<p>means that the function accepts any number of arguments of undefined type.&nbsp;&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ANSI C requires at least one argument before the ellipsis, &#8230;<\/li>\n\n\n\n<li>ANSI C requires a comma between the last required argument and the ellipsis.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Overloading_of_Function_Names\"><\/span>Overloading of Function Names<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>C (and most other languages) requires that every function have a unique name.<br>For example, if you wish to have a function that returns the absolute value of an <strong>int<\/strong>, <strong>long<\/strong>, <strong>float<\/strong> and <strong>double<\/strong>, you must give each function a different name, even though its purpose is exactly the same:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>int    iabs(int i);\nlong   labs(long l);\nfloat  fabs(float f);\ndouble dabs(double d);<\/code><\/pre>\n\n\n\n<p>which tends to cause <em>&#8216;name pollution&#8217;<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;iostream.h&gt;\n\nlong  Abs(long i)\n{\n\tcout &lt;&lt; &quot;Abs of long\\n&quot;;\n\treturn (i &gt; 0) ? i : -i;\n}\n\ndouble Abs(double d)\n{\n\tcout &lt;&lt; &quot;Abs of double\\n&quot;;\n\treturn (d &gt; 0.0) ? d : -d;\n}\n\nint main()\n{\n\tlong   lv = 5;\n\tdouble dv = -27.3;\n\n\tcout &lt;&lt; Abs(lv) &lt;&lt; '\\n';\n\tcout &lt;&lt; Abs(dv) &lt;&lt; '\\n';\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream.h&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">long<\/span><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Abs<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">long<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">i<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tcout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Abs of long<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> (i &gt; <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">) ? i : -i;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Abs<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">d<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tcout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Abs of double<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> (d &gt; <\/span><span style=\"color: #098658\">0.0<\/span><span style=\"color: #000000\">) ? d : -d;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">long<\/span><span style=\"color: #000000\">   lv = <\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> dv = -<\/span><span style=\"color: #098658\">27.3<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tcout &lt;&lt; <\/span><span style=\"color: #795E26\">Abs<\/span><span style=\"color: #000000\">(lv) &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tcout &lt;&lt; <\/span><span style=\"color: #795E26\">Abs<\/span><span style=\"color: #000000\">(dv) &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>C++ allows many functions to have the same name, which allows the same operation to be applied to different types.<\/li>\n\n\n\n<li>This is used heavily with classes.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Function_Signature\"><\/span>Function Signature<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Overloaded functions are distinguished from each other by their <em>signature<\/em><\/p>\n\n\n\n<p>A function&#8217;s signature consists of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>its name<\/li>\n\n\n\n<li>the number of its parameters<\/li>\n\n\n\n<li>the type(s) of its parameters, in order<\/li>\n\n\n\n<li>but <strong><em>not<\/em><\/strong> its return type!<\/li>\n<\/ul>\n\n\n\n<p>a function&#8217;s return value can be discarded in C\/C++, so the compiler cannot distinguish between functions which are called with the same arguments but without the call making use of any return value.<\/p>\n\n\n\n<p>The C++ compiler attempts to match a function call to its corresponding function, based on the function name and the number and type(s) of its actual arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If there is an exact match, then that is the function chosen<\/li>\n\n\n\n<li>If there is no exact match, then implicit conversions can be used, which can result in:\n<ul class=\"wp-block-list\">\n<li>no match<\/li>\n\n\n\n<li>more than one match (ambiguity)<\/li>\n\n\n\n<li>a single unique match<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><em>Other than a single match produces a compilation error<\/em><\/li>\n<\/ul>\n\n\n\n<p>The exact rules for matching a call to its overloaded function, when combined with implicit type conversions, are quite complex.<\/p>\n\n\n\n<p>Templates (covered much later in the course) complicate things still further.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Default_Values_in_Function_Prototypes\"><\/span>Default Values in Function Prototypes<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;iostream.h&gt;\n\nvoid create_window(int height = 24,\n      \t           int width  = 80)\n{     \n\t\/\/ ...\n\tcout &lt;&lt; &quot;Creating Window &quot;\n\t     &lt;&lt; height &lt;&lt; &quot; by &quot; \n\t     &lt;&lt; width  &lt;&lt; '\\n';\n      \/\/ ...\n}\n\nint main()\n{\n\tcreate_window(10, 20);\n\tcreate_window(12);\n\tcreate_window();\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream.h&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">create_window<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">height<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #098658\">24<\/span><span style=\"color: #000000\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">      \t           <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">width<\/span><span style=\"color: #000000\">  = <\/span><span style=\"color: #098658\">80<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{     <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tcout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Creating Window &quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t     &lt;&lt; height &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; by &quot;<\/span><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t     &lt;&lt; width  &lt;&lt; <\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&#39;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">      \/\/ ...<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">create_window<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">20<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">create_window<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">12<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">create_window<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>There are many situations where a function is called with an argument usually having the same value.&nbsp; It would be convenient to simply omit the parameter and have it <em>default<\/em> to a specified value. &nbsp; C++ supports this.<\/li>\n\n\n\n<li>Default values are only allowed <em>on trailing arguments<\/em>.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Unspecified_Function_Parameters\"><\/span>Unspecified Function Parameters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The checking for number and types of function parameters can be suppressed in C++:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>int noway(...);<\/strong><\/code><\/pre>\n\n\n\n<p>which is equivalent to the <em>K&amp;R-style C function declaration<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>int noway();<\/strong><\/code><\/pre>\n\n\n\n<p><strong><em><\/em><\/strong><strong><em>But this is strongly discouraged!<\/em><\/strong><\/p>\n\n\n\n<p><strong><em>It should only be used when there is no alternative!&nbsp; <\/em><\/strong><strong><em><br><\/em><\/strong><strong><em>(And there almost always is a better alternative!)<\/em><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Type-Safe_Linkage\"><\/span>Type-Safe Linkage<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/* fother.c *\/\ndouble fother_the_grommet(int x, double d)\n{\n\t\/* ... *\/\n}\n\n\/* main.c *\/\nint main()\n{\n\tint i;\t\n\ti = fother_the_grommet(); \/* Bad call! *\/\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">\/* fother.c *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">fother_the_grommet<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">double<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">d<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t\/* ... *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/* main.c *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i;\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\ti = <\/span><span style=\"color: #795E26\">fother_the_grommet<\/span><span style=\"color: #000000\">();<\/span><span style=\"color: #008000\"> \/* Bad call! *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>A major weakness of C compilers is that there is no cross-module type checking.<\/li>\n\n\n\n<li>The compiler and linker cannot detect the mismatch;&nbsp; you only find the problem at run\/debug time, when all manner of strange things are likely to happen.\n<ul class=\"wp-block-list\">\n<li><em>Type-Safe Linkage<\/em> is achieved, in most C++ compilers, by <em>name mangling<\/em> of external names, so that they convey more information, such as parameter number and types.&nbsp; Unfortunately, often, different compilers implement this incompatibly.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Calling_non-C_Functions\"><\/span>Calling non-C++ Functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>It is a goal for C++ to be able to call non-C++ routines (such as functions written in C).&nbsp; We therefore need an escape mechanism from the normal C++ calling conventions:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>extern \"C\" <\/strong>{\n       double sin(double x);\n       double cos(double x);\n       double tan(double x);\n       }     \n<strong>extern \"C\"<\/strong> {\n#include \"myfile.h\"\n}\n<\/code><\/pre>\n\n\n\n<p>Note that <em>function prototypes are still required<\/em>:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>extern \"C\" {\n    int foo(); \/* Means int foo(void); !*\/\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Reference_Parameters\"><\/span>Reference Parameters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"void swapint(int *first, int *second)\n{\n\tint temp = *first;\t\/\/ Must dereference\n\t*first = *second; \t\/\/ Must dereference\n\t*second = temp;\t    \/\/ Must dereference\n}\n\nint main()\n{\n\tint a = 1, b = 2;\n\tswapint(&amp;a, &amp;b);  \/\/ Must take address\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">swapint<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #001080\">first<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #001080\">second<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> temp = *first;<\/span><span style=\"color: #008000\">\t\/\/ Must dereference<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t*first = *second;<\/span><span style=\"color: #008000\"> \t\/\/ Must dereference<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t*second = temp;<\/span><span style=\"color: #008000\">\t    \/\/ Must dereference<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> a = <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">, b = <\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">swapint<\/span><span style=\"color: #000000\">(&amp;a, &amp;b);<\/span><span style=\"color: #008000\">  \/\/ Must take address<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Passing parameters in C can sometimes be clumsy, particularly when the parameters are <em>out<\/em> or <em>in\/out<\/em>.<\/li>\n\n\n\n<li>Both the function definitions, <em>and<\/em> the calls to them become complex.<\/li>\n\n\n\n<li>Most programming languages take care of this for you!<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"void swapint(int &amp;first, int &amp;second)\n{\n\tint temp = first;\n\tfirst = second;\n\tsecond = temp;\n}\n\nint main()\n{\n\tint a = 1, b = 2;\n\tswapint(a, b);\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">swapint<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">first<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">second<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> temp = first;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tfirst = second;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tsecond = temp;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> a = <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">, b = <\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">swapint<\/span><span style=\"color: #000000\">(a, b);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>C++ has added the <em>reference<\/em>.<\/li>\n\n\n\n<li>The C++ compiler takes care of doing the right thing to satisfy the parameter passing mechanism<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"References\"><\/span>References<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"int main()\n{\n\tint i, val = 1;\n\tint *iptr;\n\tint &amp;refval = val;\t\/\/ must be initialized!\n\tfloat data[] = {2.3, 5.2, 17.9};\n\tfloat &amp;refdata = data[2];\n\n\trefval += 2;\t\/\/ adds 2 to val\n\ti = refval;     \t\/\/ assigns val to i\n\tiptr = &refval; \t\/\/ points iptr to val\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i, val = <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> *iptr;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> &amp;refval = val;<\/span><span style=\"color: #008000\">\t\/\/ must be initialized!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">float<\/span><span style=\"color: #000000\"> data[] = {<\/span><span style=\"color: #098658\">2.3<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">5.2<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">17.9<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">float<\/span><span style=\"color: #000000\"> &amp;refdata = <\/span><span style=\"color: #001080\">data<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">];<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\trefval += <\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">;<\/span><span style=\"color: #008000\">\t\/\/ adds 2 to val<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\ti = refval;<\/span><span style=\"color: #008000\">     \t\/\/ assigns val to i<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tiptr = &amp;refval;<\/span><span style=\"color: #008000\"> \t\/\/ points iptr to val<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Reference parameters are simply a special case of a <em>reference variable<\/em>:<\/li>\n\n\n\n<li>A <em>reference<\/em> is an <em>alias<\/em> for another variable.<\/li>\n\n\n\n<li>A <em>reference<\/em> is very like a pointer, except:\n<ul class=\"wp-block-list\">\n<li>a <em>reference<\/em> must be initialized, and:<\/li>\n\n\n\n<li>once initialized, a <em>reference<\/em> cannot be made to alias another object.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>A <em>reference<\/em> is <em>not<\/em> an object<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"References_to_Pointers\"><\/span>References to Pointers<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"void swap(void **first, void **second)\n{\n\tvoid *temp = *first;\n\n\t*first = *second;\t\/\/ swap pointers\n\t*second = temp;\n}\n\nint main()\n{\n\tint   a = 1,      b = 2;\n\tint   *aptr = &amp;a, *bptr = &b;\n\tfloat f = 1.0,    g = 2.0;\n\tfloat *fptr = &amp;f, *gptr = &g;\n\n\tswap(&amp;aptr, &amp;bptr);\n\tswap(&amp;fptr, &amp;gptr);\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">**<\/span><span style=\"color: #001080\">first<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">**<\/span><span style=\"color: #001080\">second<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> *temp = *first;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t*first = *second;<\/span><span style=\"color: #008000\">\t\/\/ swap pointers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t*second = temp;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">   a = <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">,      b = <\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">   *aptr = &amp;a, *bptr = &amp;b;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">float<\/span><span style=\"color: #000000\"> f = <\/span><span style=\"color: #098658\">1.0<\/span><span style=\"color: #000000\">,    g = <\/span><span style=\"color: #098658\">2.0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">float<\/span><span style=\"color: #000000\"> *fptr = &amp;f, *gptr = &amp;g;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(&amp;aptr, &amp;bptr);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(&amp;fptr, &amp;gptr);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>In C, we can often generalize functions by using arguments of type <em>pointer to pointer to <\/em>void:<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"void swap(void *&amp;first, void *&amp;second)\n{\n\tvoid *temp = first;\n\n\tfirst = second;\t\/\/ swap pointers\n\tsecond = temp;\n}\n\nint main()\n{\n\tint   a = 1,      b = 2;\n\tvoid  *aptr = &amp;a, *bptr = &b;\n\tfloat f = 1.0,    g = 2.0;\n\tvoid *fptr = &amp;f,  *gptr = &g;\n\n\tswap(aptr, bptr);\n\tswap(fptr, gptr);\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*&amp;<\/span><span style=\"color: #001080\">first<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*&amp;<\/span><span style=\"color: #001080\">second<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> *temp = first;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tfirst = second;<\/span><span style=\"color: #008000\">\t\/\/ swap pointers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tsecond = temp;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">   a = <\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">,      b = <\/span><span style=\"color: #098658\">2<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\">  *aptr = &amp;a, *bptr = &amp;b;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">float<\/span><span style=\"color: #000000\"> f = <\/span><span style=\"color: #098658\">1.0<\/span><span style=\"color: #000000\">,    g = <\/span><span style=\"color: #098658\">2.0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> *fptr = &amp;f,  *gptr = &amp;g;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(aptr, bptr);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(fptr, gptr);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>C++ also supports <em>references to pointers<\/em>, which provides a better solution.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"void swap(int &amp;first, int &amp;second)\n{\n\tint temp = first;\n\tfirst = second;\n\tsecond = temp;\n}\n\nint main()\n{\n\tint a = 10;\n\tunsigned int b = 20;\n\tswap(a, b);\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">first<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #001080\">second<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> temp = first;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tfirst = second;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tsecond = temp;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> a = <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">unsigned<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> b = <\/span><span style=\"color: #098658\">20<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(a, b);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Note that you have to be very careful mixing types with references;&nbsp; if the type does not match exactly:<\/li>\n\n\n\n<li>Here, the compiler does the equivalent of:<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"int main()\n{\n\tint a = 10;\n\tunsigned int b = 20;\n\tint T = int(b);\n\tswap(a, T);\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> a = <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">unsigned<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> b = <\/span><span style=\"color: #098658\">20<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> T = <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">(b);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">swap<\/span><span style=\"color: #000000\">(a, T);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>Note the careful datatype matching in the previous example!<\/p>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Functions_Returning_References\"><\/span>Functions Returning References<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"struct Location\n{\n\tint x, y;\n};\n\nLocation &amp;Position(int newx, int newy)\n{\n\tstatic Location myloc;\n\n\tmyloc.x = newx;\n\tmyloc.y = newy;\n\treturn myloc;\n}\n\nint main()\n{\n\tLocation here = {0, 0};\n\there = Position(10, 20);\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">struct<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Location<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> x, y;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">Location<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">&amp;<\/span><span style=\"color: #795E26\">Position<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">newx<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">newy<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> Location myloc;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #001080\">myloc<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">x<\/span><span style=\"color: #000000\"> = newx;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #001080\">myloc<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #001080\">y<\/span><span style=\"color: #000000\"> = newy;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> myloc;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tLocation here = {<\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\there = <\/span><span style=\"color: #795E26\">Position<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">20<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Functions may also be declared as returning references:<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Note that the <strong>static<\/strong> storage specifier is extremely important here!&nbsp; Why?<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Advantages_of_References\"><\/span>Advantages of References<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Advantages of <em>references<\/em> are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>They <em>clean up syntax <\/em>significantly.<\/li>\n\n\n\n<li>They place the <em>responsibility<\/em> for proper argument passing on the <em>compiler<\/em>, and on the <em>supplier<\/em> of a function, rather than the <em>client<\/em>.<\/li>\n\n\n\n<li>They typically are more efficient for function argument passing, especially for large objects.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_inline_Specifier\"><\/span>The inline Specifier<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We encourage <em>procedural abstraction<\/em>, but this can cause programs to run more slowly if you use lots of functions.<\/p>\n\n\n\n<p>C&#8217;s solution to this was to define a macro &#8216;function&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>#define min(x, y) ((x) &lt; (y) ? (x) : (y))<\/code><\/pre>\n\n\n\n<p>But this is considered undesireable because:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Lack of Type Checking: Macros do not undergo type-checking during compilation. &#8230;<\/li>\n\n\n\n<li>Difficulty in Debugging: Macros can make code harder to debug due to their inline nature. &#8230;<\/li>\n\n\n\n<li>Potential for Code Bloat: Macros can result in code duplication when used extensively throughout the program.<\/li>\n<\/ul>\n\n\n\n<p>C++ helps solve this problem by supporting <strong>inline<\/strong> functions:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>inline int min(int i, int j)\n{\n\treturn (i &lt; j) ? i : j;\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>inline<\/strong> is only a <em>hint<\/em> to the compiler<\/li>\n\n\n\n<li><strong>inline<\/strong> causes the function to have <em>default internal linkage<\/em><\/li>\n\n\n\n<li><strong>inline<\/strong> becomes very important within classes<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_new_and_delete_Operators\"><\/span>The new and delete Operators<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n\nint main()\n{\n\tint *iptr;\n\n\tiptr = malloc(sizeof(int));\t\/\/ check return value!\n\t*iptr = 5;\n\tprintf(&quot;%d\\n&quot;, *iptr);\n\tfree(iptr);\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;stdio.h&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;stdlib.h&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> *iptr;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tiptr = <\/span><span style=\"color: #795E26\">malloc<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">sizeof<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">));<\/span><span style=\"color: #008000\">\t\/\/ check return value!<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t*iptr = <\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">printf<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #001080\">%d<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">, *iptr);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">free<\/span><span style=\"color: #000000\">(iptr);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>C does not have support in the language for dynamically allocated objects;&nbsp; you have to call the <strong>malloc()<\/strong> (or <strong>calloc()<\/strong>, etc.) function to allocate space, make sure the space is initialized, and eventually deallocate the space using the <strong>free()<\/strong> function:<\/li>\n\n\n\n<li>Modern languages like Ada support this in the language:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>type INT_PTR is access INT;\nMY_INT : INT_PTR;\n       ...\nMY_INT := new INT_PTR;\n<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;iostream&gt;\n\nint main()\n{\n  int *iptr = new int;\n  char *string = new char[40];\n  \/\/ Check for success\n  \/\/ (Really need to catch exception)\n  if ((iptr == NULL) || (string == NULL))\n  {\n    std::cerr &lt;&lt; &quot;No memory&quot; \n              &lt;&lt; std::endl;\n    return 0;\n  }\n  *iptr = 5;\n  std::cout &lt;&lt; *iptr;\n  delete iptr;\t\/\/ use this on scalars\n  delete [] string; \/\/ use this on arrays\n  return 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> *iptr = <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *string = <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #098658\">40<\/span><span style=\"color: #000000\">];<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Check for success<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ (Really need to catch exception)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> ((iptr == <\/span><span style=\"color: #0000FF\">NULL<\/span><span style=\"color: #000000\">) || (string == <\/span><span style=\"color: #0000FF\">NULL<\/span><span style=\"color: #000000\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cerr &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;No memory&quot;<\/span><span style=\"color: #000000\"> <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">              &lt;&lt; <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::endl;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  *iptr = <\/span><span style=\"color: #098658\">5<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">std<\/span><span style=\"color: #000000\">::cout &lt;&lt; *iptr;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #000000\"> iptr;<\/span><span style=\"color: #008000\">\t\/\/ use this on scalars<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #AF00DB\">[]<\/span><span style=\"color: #000000\"> string;<\/span><span style=\"color: #008000\"> \/\/ use this on arrays<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>C++ provides similar <em>operators<\/em>, <strong>new<\/strong> and <strong>delete<\/strong>:<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<p><strong>new<\/strong> and <strong>delete<\/strong> are C++ <em>operators<\/em>, and are <em>typed<\/em>.&nbsp; The typing is particularly valuable.<\/p>\n\n\n\n<p>For a scalar, use the simple forms of <strong>new<\/strong> and <strong>delete<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>int *iptr = new int;&nbsp; \/* ... *\/ delete iptr;<\/strong><\/code><\/pre>\n\n\n\n<p>For an array, specify size to the new operator:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>int *iarrayptr = new int&#91;5];<\/strong><\/code><\/pre>\n\n\n\n<p>For an array, use the <strong>delete[]<\/strong> form of the delete operator, and do <em>not<\/em> specify the size of the array:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>delete&#91;] iarrayptr;<\/strong><\/code><\/pre>\n\n\n\n<p>Space is allocated from the <em>free store<\/em><em>(heap)<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If the space requested is not available, <strong>new<\/strong> returns a <strong>NULL<\/strong> pointer, just like <strong>malloc<\/strong>, except that:<\/li>\n\n\n\n<li>The C++ ANSI\/ISO committee changed the behavior of new to <em>raise an exception <\/em>if it fails to allocate space.<\/li>\n\n\n\n<li>Note that some compilers (including Microsoft Visual C++) have maintained the older behavior, for backwards compatibility.&nbsp; You can explicitly choose the new behavior with the use of a compiler switch.<\/li>\n<\/ul>\n\n\n\n<p><strong>new<\/strong> and <strong>delete<\/strong> become particularly important with classes.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"#include &lt;new.h&gt;\n#include &lt;iostream.h&gt;\n\t     \nvoid heap_problem()\n{\n\tcerr &lt;&lt; &quot;operator new failed\\n&quot;;\n    \/* ... *\/\n}\t \n\nint main()\n{          \n\tvoid (*old_new_handler)();\n    \/* ... *\/\n\told_new_handler = \n\t\t\tset_new_handler(heap_problem);\n    \/* ...  *\/\n\tset_new_handler(old_new_handler);\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;new.h&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&lt;iostream.h&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t     <\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">heap_problem<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\tcerr &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;operator new failed<\/span><span style=\"color: #EE0000\">\\n<\/span><span style=\"color: #A31515\">&quot;<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/* ... *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}\t <\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{          <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> (*<\/span><span style=\"color: #001080\">old_new_handler<\/span><span style=\"color: #000000\">)();<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/* ... *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\told_new_handler = <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t\t\t<\/span><span style=\"color: #795E26\">set_new_handler<\/span><span style=\"color: #000000\">(heap_problem);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">    \/* ...  *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #795E26\">set_new_handler<\/span><span style=\"color: #000000\">(old_new_handler);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul class=\"wp-block-list\">\n<li>Unlike <strong>malloc()<\/strong>, <strong>new<\/strong> can handle failures to allocate space:<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"void_and_Functions_Returning_void\"><\/span>void* and Functions Returning void<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>ANSI C includes two concepts which came from C++:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pointers to <strong>void<\/strong> (meaning <em>unknown type<\/em>):<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>void *generic_ptr;<\/strong><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Functions returning <strong>void<\/strong> (meaning <em>no value<\/em>)<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>void my_procedure(int errogate, \n                  double cross);\n{\n    \/* ... *\/\n}\n<\/strong><\/code><\/pre>\n\n\n\n<p>ANSI C allows the assignment of a pointer of type <strong>void*<\/strong> to a pointer of any other type.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>C++ is stricter; it requires an explicit cast:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"int main()\t       \n{\n\tint i;\n\tvoid *vptr = &i;\n\tchar *cptr = vptr; \t\/\/ allowed in ANSI C\n\tchar *cptr = (char*) vptr;\t\/\/ required by C++\n\t   \/* ... *\/\n\treturn 0;\n}\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">()\t       <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> i;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> *vptr = &amp;i;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *cptr = vptr;<\/span><span style=\"color: #008000\"> \t\/\/ allowed in ANSI C<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *cptr = (<\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">*) vptr;<\/span><span style=\"color: #008000\">\t\/\/ required by C++<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\t   \/* ... *\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Overview of Small Enhancements New Keywords and Reserved Words C keywords C++ keywords added: Line-Style Comments Some Differences Due to Stronger Typing In C++, literal character constants (&#8216;a&#8217;) are of type char; in C, they are of type int: In C++, the use of an undeclared function is an error; in C, an undeclared function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1871,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-918","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/918","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/comments?post=918"}],"version-history":[{"count":15,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/918\/revisions"}],"predecessor-version":[{"id":1681,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/918\/revisions\/1681"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1871"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=918"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}