{"id":1157,"date":"2024-08-22T18:58:54","date_gmt":"2024-08-22T18:58:54","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/?page_id=1157"},"modified":"2025-02-10T22:45:46","modified_gmt":"2025-02-10T22:45:46","slug":"const-reference-and-static-members","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/topics\/c-details\/classes\/const-reference-and-static-members\/","title":{"rendered":"const, Reference, and static Members"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"const_Reference_and_static_Members\"><\/span>const, Reference, and static Members<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"const_Members\"><\/span>const Members<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>C++ has:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>const<\/strong> data members<\/li>\n\n\n\n<li><strong>const<\/strong> member functions<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"const_Data_Members\"><\/span>const Data Members<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>It should be clear that a <strong>const<\/strong> <em>data member<\/em> of a class:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><em>must be initialized<\/em> (possibly via a default constructor) in the containing class constructors<\/li>\n\n\n\n<li><em>may not change <\/em>during the lifetime of the class instance (that&#8217;s the meaning of <strong>const<\/strong>)<\/li>\n<\/ul>\n\n\n\n<p>This can be useful;&nbsp; it is too seldom used!<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"const_Member_Functions\"><\/span>const Member Functions<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>So, what is a <strong>const<\/strong><em> <\/em><em>member function<\/em>?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It&#8217;s a member function that promises not to modify its class instance.<\/li>\n\n\n\n<li>So, all member functions which are accessor functions should be declared const.<\/li>\n\n\n\n<li>Also, member functions like Print() (etc.) should be declared const.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s the syntax:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>class Complex\n{\n\t\/\/ ...\n\tvoid Print() <strong>const<\/strong>;\n\tdouble GetReal() <strong>const<\/strong> { return real; }\n\tdouble GetImag() <strong>const<\/strong> { return imag; }\n\t\/\/ ...\n};\n<\/code><\/pre>\n\n\n\n<p>where the <strong>const<\/strong> is placed <em>after<\/em> the function argument list, and <em>before<\/em> the function body.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example:<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>Let&#8217;s use our <strong>Complex<\/strong> class from the previous <code>Complex<\/code>\/<code>ComplexLine<\/code> example:<\/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-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  main.cpp\n\/\/  Member Class Objects\n\/\/\n\/\/  Created by Bryan Higgs on 8\/22\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &quot;Complex.h&quot;\n\nint main(int argc, const char * argv[]) \n{\n  ComplexLine line1;\n  line1.Print();\n  \n  ComplexLine line2(Complex(2.0, 1.5),\n                    Complex(3.9, 2.5));\n  line2.Print();\n  \n  const Complex c(3.4, 5.6);  \t\/\/ OK\n\tc.Print();\t\n\t\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: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Member Class Objects<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 8\/22\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/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 style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;Complex.h&quot;<\/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 style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/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\">  ComplexLine line1;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">line1<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">Print<\/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\">  ComplexLine <\/span><span style=\"color: #795E26\">line2<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">2.0<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">1.5<\/span><span style=\"color: #000000\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                    <\/span><span style=\"color: #795E26\">Complex<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3.9<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">2.5<\/span><span style=\"color: #000000\">));<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">line2<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">Print<\/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\">const<\/span><span style=\"color: #000000\"> Complex <\/span><span style=\"color: #795E26\">c<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #098658\">3.4<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">5.6<\/span><span style=\"color: #000000\">);<\/span><span style=\"color: #008000\">  \t\/\/ OK<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">\t<\/span><span style=\"color: #001080\">c<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">Print<\/span><span style=\"color: #000000\">();\t<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">\t<\/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<p>But it gives a compile time error:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>main.cpp:21:3 'this' argument to member function 'Print' has type 'const Complex', but function is not marked const\n\n<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>Since the C++ compiler takes us seriously when we say <strong>const<\/strong>, it doesn&#8217;t know that the <strong>Print()<\/strong> member function won&#8217;t change the instance.<\/p>\n\n\n\n<p>By adding <strong>const<\/strong>, we are telling the compiler that the member function will not change anything in the instance.<\/p>\n\n\n\n<p>Note that the compiler will check the member function implementation to ensure that it does not change anything!<\/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\" style=\"flex-basis:60%\">\n<p>We fix the problem by simply adding &#8216;const&#8217; to the declaration of member function Print:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-width:calc(1 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"  void Print() const;\" 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: #000000\">  <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Print<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\">;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>&#8230; and also to its definition:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"void Complex::Print() const\n{\n  std::cout &lt;&lt; &quot;Complex: [&quot; &lt;&lt; m_real &lt;&lt; &quot;, &quot; &lt;&lt; m_imag &lt;&lt; &quot;]&quot;;\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: #267F99\">Complex<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">Print<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #0000FF\">const<\/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\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Complex: [&quot;<\/span><span style=\"color: #000000\"> &lt;&lt; m_real &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;, &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; m_imag &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;]&quot;<\/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\" style=\"flex-basis:40%\">\n<p>The program now compiles and runs correctly.<br><\/p>\n<\/div>\n<\/div>\n\n\n\n<p>The <strong>this<\/strong> pointer for a <strong>const<\/strong> member function of class <strong>T<\/strong> is declared thusly:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>const T * const this;\t\/\/ meaning?<\/strong><\/code><\/pre>\n\n\n\n<p>which enforces the rules for code inside the const member function.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Breaking_the_Rules\"><\/span>Breaking the Rules<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>There are times when you would like a member function to be:<\/p>\n\n\n\n<p>&#8216;physically <strong>const<\/strong>&#8216;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It truly does not change its instance.<\/li>\n\n\n\n<li>Typical, straightforward usage.<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Logical_const-ness\"><\/span>Logical const-ness<span class=\"ez-toc-section-end\"><\/span><\/h5>\n\n\n\n<p>There are other times when you would like a member function to be:<\/p>\n\n\n\n<p>&#8216;logically <strong>const<\/strong>&#8216;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>It appears to the outside world not to change its instance<\/li>\n\n\n\n<li>In fact, it does change something in the instance (an implementation detail?)<\/li>\n\n\n\n<li>To do this, one must explicitly cast the <strong>this<\/strong> pointer:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code><strong>double Real() const\n{\n    ((Complex *)this)-&gt;real_accesses++; \/\/ Count of accesses (instrumentation)\n    return real;\n}<\/strong><\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>Clearly, this is something to do only if circumstances warrant. It should not be normal usage &#8212; especially not to get your program to compile in order to get around a problem you don&#8217;t yet understand.<\/em><\/p>\n\n\n\n<p>It is typically useful for keeping track of things like method invocation counts, and similar things.<\/p>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"mutable_Data_Members\"><\/span>mutable Data Members<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>The C++ standard has now blessed this as a supported practice. They have introduced the keyword mutable, that allows a member variable of a <strong>const<\/strong> object to be modified. It\u2019s used to specify that a particular data member of a <strong>const<\/strong> object can be modified without affecting the object\u2019s <strong>const<\/strong>-ness. <\/p>\n\n\n\n<p>Here&#8217;s an example:<\/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-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  Toys.hpp\n\/\/  const, volatile, mutable\n\/\/\n\/\/  Created by Bryan Higgs on 2\/10\/25.\n\/\/\n\n#ifndef Toys_hpp\n#define Toys_hpp\n\n#include &lt;string&gt;\n\nclass Toy\n{\npublic:\n  Toy(std::string&amp; name);\n  \n  std::string&amp; getName() const;\n  \n  int getAccessCount() const;\n    \nprivate:\n    std::string&amp; m_name;\n    mutable int m_accessCount = 0;\n};\n\n#endif \/* Toys_hpp *\/\" 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\">\/\/  Toys.hpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  const, volatile, mutable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 2\/10\/25.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">#<\/span><span style=\"color: #001080\">ifndef<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">Toys_hpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">#<\/span><span style=\"color: #001080\">define<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">Toys_hpp<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">#<\/span><span style=\"color: #001080\">include<\/span><span style=\"color: #000000\"> &lt;<\/span><span style=\"color: #001080\">string<\/span><span style=\"color: #000000\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Toy<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #001080\">public<\/span><span style=\"color: #000000\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #267F99\">Toy<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\">&amp; <\/span><span style=\"color: #267F99\">name<\/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: #001080\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\">&amp; <\/span><span style=\"color: #267F99\">getName<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #267F99\">const<\/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: #001080\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">getAccessCount<\/span><span style=\"color: #000000\">() const;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #001080\">private<\/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\">::<\/span><span style=\"color: #267F99\">string<\/span><span style=\"color: #000000\">&amp; <\/span><span style=\"color: #267F99\">m_name<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">    <\/span><span style=\"color: #001080\">mutable<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">m_accessCount<\/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>\n<span class=\"line\"><span style=\"color: #000000\">#<\/span><span style=\"color: #001080\">endif<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008000\">\/* Toys_hpp *\/<\/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<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,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\/\/  Toys.cpp\n\/\/  const, volatile, mutable\n\/\/\n\/\/  Created by Bryan Higgs on 2\/10\/25.\n\/\/\n\n#include &quot;Toys.hpp&quot;\n\nToy::Toy(std::string&amp; name)\n  : m_name(name)\n{}\n\nstd::string&amp; Toy::getName() const\n{\n  m_accessCount++;\n  return m_name;\n}\n\nint Toy::getAccessCount() const\n{\n  return m_accessCount;\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\">\/\/  Toys.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  const, volatile, mutable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 2\/10\/25.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">#<\/span><span style=\"color: #001080\">include<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #A31515\">&quot;Toys.hpp&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">Toy::<\/span><span style=\"color: #795E26\">Toy<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">std<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #001080\">string<\/span><span style=\"color: #000000\">&amp; <\/span><span style=\"color: #001080\">name<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  : <\/span><span style=\"color: #795E26\">m_name<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">name<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">std::<\/span><span style=\"color: #001080\">string<\/span><span style=\"color: #000000\">&amp; Toy::<\/span><span style=\"color: #795E26\">getName<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #0000FF\">const<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">m_accessCount<\/span><span style=\"color: #000000\">++;<\/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: #001080\">m_name<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #001080\">int<\/span><span style=\"color: #000000\"> Toy::<\/span><span style=\"color: #795E26\">getAccessCount<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #0000FF\">const<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/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: #001080\">m_accessCount<\/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\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>Here&#8217;s a main program to run this code:<\/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-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * .875rem);--cbp-line-highlight-color:rgba(0, 0, 0, 0.2);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span role=\"button\" tabindex=\"0\" data-code=\"\/\/\n\/\/  main.cpp\n\/\/  const, volatile, mutable\n\/\/\n\/\/  Created by Bryan Higgs on 2\/10\/25.\n\/\/\n\n#include &lt;iostream&gt;\n#include &lt;array&gt;\n#include &quot;Toys.hpp&quot;\n\nvoid listToys(const Toy &amp;firstToy, const Toy &amp;secondToy)\n{\n  std::cout &lt;&lt; &quot;Toy [1] : &quot; &lt;&lt; firstToy.getName() &lt;&lt; std::endl;\n  std::cout &lt;&lt; &quot;Toy [2] : &quot; &lt;&lt; secondToy.getName() &lt;&lt; std::endl;\n}\n\nint main(int argc, const char * argv[]) \n{\n  static std::string Train(&quot;Train&quot;);\n  const Toy train(Train);\n\n  static std::string Barbie(&quot;Barbie&quot;);\n  const Toy doll(Barbie);\n  \n  listToys(train, doll);\n  \n  std::cout &lt;&lt; &quot;Number of accesses to: &quot; &lt;&lt; train.getName() &lt;&lt; &quot; = &quot; &lt;&lt; train.getAccessCount() &lt;&lt; std::endl;\n  std::cout &lt;&lt; &quot;Number of accesses to: &quot; &lt;&lt; doll.getName() &lt;&lt; &quot; = &quot; &lt;&lt; doll.getAccessCount() &lt;&lt; std::endl;\n\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: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  const, volatile, mutable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 2\/10\/25.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">#<\/span><span style=\"color: #001080\">include<\/span><span style=\"color: #000000\"> &lt;<\/span><span style=\"color: #001080\">iostream<\/span><span style=\"color: #000000\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">#<\/span><span style=\"color: #001080\">include<\/span><span style=\"color: #000000\"> &lt;<\/span><span style=\"color: #001080\">array<\/span><span style=\"color: #000000\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">#<\/span><span style=\"color: #001080\">include<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #A31515\">&quot;Toys.hpp&quot;<\/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\">listToys<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">Toy<\/span><span style=\"color: #000000\"> &amp;<\/span><span style=\"color: #001080\">firstToy<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">Toy<\/span><span style=\"color: #000000\"> &amp;<\/span><span style=\"color: #001080\">secondToy<\/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\">  std::<\/span><span style=\"color: #001080\">cout<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Toy [1] : &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #001080\">firstToy<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">getName<\/span><span style=\"color: #000000\">() &lt;&lt; std::<\/span><span style=\"color: #001080\">endl<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  std::<\/span><span style=\"color: #001080\">cout<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Toy [2] : &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #001080\">secondToy<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">getName<\/span><span style=\"color: #000000\">() &lt;&lt; std::<\/span><span style=\"color: #001080\">endl<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #001080\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">main<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">char<\/span><span style=\"color: #000000\"> * <\/span><span style=\"color: #001080\">argv<\/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: #001080\">static<\/span><span style=\"color: #000000\"> std::<\/span><span style=\"color: #001080\">string<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Train<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Train&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0070C1\">Toy<\/span><span style=\"color: #000000\"> train(Train);<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #001080\">static<\/span><span style=\"color: #000000\"> std::<\/span><span style=\"color: #001080\">string<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Barbie<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Barbie&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line cbp-line-highlight\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0070C1\">Toy<\/span><span style=\"color: #000000\"> doll(Barbie);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">listToys<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #001080\">train<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">doll<\/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\">  std::<\/span><span style=\"color: #001080\">cout<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Number of accesses to: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #001080\">train<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">getName<\/span><span style=\"color: #000000\">() &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #001080\">train<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">getAccessCount<\/span><span style=\"color: #000000\">() &lt;&lt; std::<\/span><span style=\"color: #001080\">endl<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  std::<\/span><span style=\"color: #001080\">cout<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Number of accesses to: &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #001080\">doll<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">getName<\/span><span style=\"color: #000000\">() &lt;&lt; <\/span><span style=\"color: #A31515\">&quot; = &quot;<\/span><span style=\"color: #000000\"> &lt;&lt; <\/span><span style=\"color: #001080\">doll<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #795E26\">getAccessCount<\/span><span style=\"color: #000000\">() &lt;&lt; std::<\/span><span style=\"color: #001080\">endl<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><\/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<p>Here is what the main program outputs:<\/p>\n\n\n\n<p>Toy [1] : Train<br>Toy [2] : Barbie<br>Number of accesses to: Train = 2<br>Number of accesses to: Barbie = 2<br>Program ended with exit code: 0<\/p>\n\n\n\n<p>Note the use of the <strong>mutable<\/strong> keyword in the <strong>Toys.hpp<\/strong> header file, and the use of <strong>const<\/strong> in the main program.<\/p>\n<\/div>\n<\/div>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p id=\"46a8\">The mutable keyword is useful in several scenarios:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Caching<\/li>\n\n\n\n<li>Lazy evaluation<\/li>\n\n\n\n<li>Thread synchronization<\/li>\n\n\n\n<li>Maintaining logical <strong>const<\/strong>-ness<\/li>\n<\/ol>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"static_Members\"><\/span>static Members<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>A class may contain <strong>static<\/strong> members.<\/p>\n\n\n\n<p>A <strong>static <\/strong><strong><em>data member<\/em><\/strong><em>:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Has only <em>a single instance per class <\/em>(actually per class hierarchy)<\/li>\n\n\n\n<li>Is shared by all instances of its class<\/li>\n\n\n\n<li>Exists even if no instances of that class (yet) exist.<\/li>\n\n\n\n<li><em>Must be initialized<\/em> outside its class<\/li>\n<\/ul>\n\n\n\n<p>A <strong>static <\/strong><strong><em>member function<\/em><\/strong><em>:<\/em><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Is not associated with any specific instance of its class (it is associated with the class, not an instance)<\/li>\n\n\n\n<li>Can be called whether or not any instances of the class (yet) exist<\/li>\n\n\n\n<li>Does not have any <strong>this<\/strong> pointer (it&#8217;s not associated with an instance)<\/li>\n<\/ul>\n\n\n\n<p><strong>static<\/strong> members are still subject to access control (<strong>private<\/strong>\/<strong>public<\/strong>)<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Example-2\"><\/span>Example:<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>Here is a <code>Person<\/code> class that collects a population of people, and reports on its current state, giving the current population, and a census of the people in that population:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>NOTE<\/strong>: This class uses a simple implementation of a linked list to keep track of the people in its current population. I do not recommend this approach for your code; C++ has, in its Standard Template Library (STL) many useful implementations of structures and algorithms, including a linked list, and many more. It would be far better to use an STL linked list, or similar.<\/p>\n\n\n\n<p>The problem is that we haven&#8217;t yet learned how to use the STL, and that takes a while to master. <\/p>\n\n\n\n<p>All in due time&#8230;<\/p>\n<\/blockquote>\n\n\n\n<p>Note that this class contains three static data members (<code>first<\/code>, <code>last<\/code>, and <code>count<\/code>) to keep track of the population and census, and two static member functions that allow the reporting of the population and census.<\/p>\n\n\n\n<p>Remember that a single instance of each of these static data members and static member functions exists, independent of how many instances of the <code>Person<\/code> class exist, or even if there are none.<\/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-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,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\/\/  Person.hpp\n\/\/  Static Members\n\/\/\n\/\/  Created by Bryan Higgs on 8\/23\/24.\n\/\/\n\n#ifndef Person_hpp\n#define Person_hpp\n\nclass Person\n{\npublic:\n  \/\/ static member functions\n  static unsigned int Population() { return count; }\n  static void Census();\n\n  \/\/ Non-static member functions\n  const char *Name() const     { return name; }\n\n  \/\/ No default constructor\n    \n  \/\/ Constructor with one argument\n  Person(const char *n);\n    \n  \/\/ Destructor\n  ~Person();\n\nprivate:\n  \/\/ static data members\n  static Person       *first;    \/\/ List header\n  static Person       *last;    \/\/  ...\n    \n  static unsigned int  count;    \/\/ Number in list\n\n  \/\/ Non-static data members\n  Person              *next;    \/\/ Item in list\n  Person              *prev;    \/\/  ...\n\n  char                *name;    \/\/ Person's name\n};\n\n#endif \/* Person_hpp *\/\" 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\">\/\/  Person.hpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Static Members<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 8\/23\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#ifndef<\/span><span style=\"color: #0000FF\"> Person_hpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#define<\/span><span style=\"color: #0000FF\"> Person_hpp<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">class<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Person<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ static member functions<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">unsigned<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Population<\/span><span style=\"color: #000000\">() { <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> count; }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Census<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Non-static member functions<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #795E26\">Name<\/span><span style=\"color: #000000\">() <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\">     { <\/span><span style=\"color: #AF00DB\">return<\/span><span style=\"color: #000000\"> name; }<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ No default constructor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Constructor with one argument<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">Person<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #001080\">n<\/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\">  \/\/ Destructor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">~Person<\/span><span style=\"color: #000000\">();<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">private:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ static data members<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> Person       *first;<\/span><span style=\"color: #008000\">    \/\/ List header<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">static<\/span><span style=\"color: #000000\"> Person       *last;<\/span><span style=\"color: #008000\">    \/\/  ...<\/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\">static<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">unsigned<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\">  count;<\/span><span style=\"color: #008000\">    \/\/ Number in list<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ Non-static data members<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Person              *next;<\/span><span style=\"color: #008000\">    \/\/ Item in list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Person              *prev;<\/span><span style=\"color: #008000\">    \/\/  ...<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\">                *name;<\/span><span style=\"color: #008000\">    \/\/ Person&#39;s name<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#endif<\/span><span style=\"color: #008000\"> \/* Person_hpp *\/<\/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<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,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\/\/  Person.cpp\n\/\/  Static Members\n\/\/\n\/\/  Created by Bryan Higgs on 8\/23\/24.\n\/\/\n\n#include &lt;stdio.h&gt;\n#include &lt;iostream&gt;\n#include &quot;Person.hpp&quot;\n\n\/\/ Initialize static data members\nPerson *Person::first = 0;\nPerson *Person::last  = 0;\nunsigned int Person::count = 0;\n\n\/\/ Constructor\nPerson::Person(const char *n)\n  : name(new char[strlen(n) + 1])\n{\n  \/\/ Insert this into the linked\n  \/\/ list, at the end of the list.\n  if (first == 0)   \/\/ first in list?\n  {\n    first = this;\n    last  = this;\n    prev  = 0;\n  }\n  else\n  {\n    last-&gt;next = this;\n    prev = last;\n    last = this;\n  }\n  next = 0;\n  count++;    \/\/ one more in list\n  strcpy(name, n);\n}\n\n\/\/ Destructor\nPerson::~Person()\n{\n  \/\/ Remove this from the linked list\n  if (prev == 0)  \/\/ first in list?\n     first = next;\n  else\n     prev-&gt;next = next;\n  if (next == 0)  \/\/ last in list?\n     last = prev;\n  else\n     next-&gt;prev = prev;\n  --count;      \/\/ one fewer in list\n  delete [] name;\n}\n\n\/\/ static member function\nvoid Person::Census()\n{\n  std::cout &lt;&lt; &quot;Current census:&quot; &lt;&lt; std::endl;\n  for (Person *p = first; p != 0; p = p-&gt;next)\n  {\n     std::cout &lt;&lt; p-&gt;Name() &lt;&lt; std::endl;\n  }\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\">\/\/  Person.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Static Members<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 8\/23\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/span>\n<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;iostream&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;Person.hpp&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Initialize static data members<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">Person *<\/span><span style=\"color: #267F99\">Person<\/span><span style=\"color: #000000\">::first = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">Person *<\/span><span style=\"color: #267F99\">Person<\/span><span style=\"color: #000000\">::last  = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">unsigned<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Person<\/span><span style=\"color: #000000\">::count = <\/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\">\/\/ Constructor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">Person<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">Person<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> *n)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  : <\/span><span style=\"color: #795E26\">name<\/span><span style=\"color: #000000\">(<\/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: #795E26\">strlen<\/span><span style=\"color: #000000\">(n) + <\/span><span style=\"color: #098658\">1<\/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\">  \/\/ Insert this into the linked<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">  \/\/ list, at the end of the list.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (first == <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #008000\">   \/\/ first in list?<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    first = <\/span><span style=\"color: #0000FF\">this<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    last  = <\/span><span style=\"color: #0000FF\">this<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    prev  = <\/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\">  <\/span><span style=\"color: #AF00DB\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #001080\">last<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #001080\">next<\/span><span style=\"color: #000000\"> = <\/span><span style=\"color: #0000FF\">this<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    prev = last;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    last = <\/span><span style=\"color: #0000FF\">this<\/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\">  next = <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  count++;<\/span><span style=\"color: #008000\">    \/\/ one more in list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">strcpy<\/span><span style=\"color: #000000\">(name, n);<\/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\">\/\/ Destructor<\/span><\/span>\n<span class=\"line\"><span style=\"color: #267F99\">Person<\/span><span style=\"color: #000000\">::~<\/span><span style=\"color: #795E26\">Person<\/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\">  \/\/ Remove this from the linked list<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (prev == <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #008000\">  \/\/ first in list?<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">     first = next;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">     <\/span><span style=\"color: #001080\">prev<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #001080\">next<\/span><span style=\"color: #000000\"> = next;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">if<\/span><span style=\"color: #000000\"> (next == <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #008000\">  \/\/ last in list?<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">     last = prev;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">else<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">     <\/span><span style=\"color: #001080\">next<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #001080\">prev<\/span><span style=\"color: #000000\"> = prev;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  --count;<\/span><span style=\"color: #008000\">      \/\/ one fewer in list<\/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\"> name;<\/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\">\/\/ static member function<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #267F99\">Person<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">Census<\/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\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Current census:&quot;<\/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\">for<\/span><span style=\"color: #000000\"> (Person *p = first; p != <\/span><span style=\"color: #098658\">0<\/span><span style=\"color: #000000\">; p = <\/span><span style=\"color: #001080\">p<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #001080\">next<\/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\">::cout &lt;&lt; <\/span><span style=\"color: #001080\">p<\/span><span style=\"color: #000000\">-&gt;<\/span><span style=\"color: #795E26\">Name<\/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>\n<span class=\"line\"><span style=\"color: #000000\">}<\/span><\/span><\/code><\/pre><\/div>\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<p>Here&#8217;s a test program for trying this out:<\/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-NL.ttf\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono-NL,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\/\/  main.cpp\n\/\/  Static Members\n\/\/\n\/\/  Created by Bryan Higgs on 8\/23\/24.\n\/\/\n\n#include &lt;iostream&gt;\n#include &quot;Person.hpp&quot;\n\n\/\/ Regular function to report on the current population\nvoid report()\n{\n  std::cout &lt;&lt; &quot;Current population: &quot;\n        &lt;&lt; Person::Population() &lt;&lt; std::endl;\n  Person::Census();\n}\n\nvoid sub()\n{\n  Person bs(&quot;Bjarne Stroustrup&quot;);\n  Person ed(&quot;Edsger Dijkstra&quot;);\n  report();\n}\n\nint main(int argc, const char * argv[])\n{\n  std::cout &lt;&lt; &quot;report();&quot; &lt;&lt; std::endl;\n  report();\n  std::cout &lt;&lt; &quot;sub();&quot; &lt;&lt; std::endl;\n  sub();\n  std::cout &lt;&lt; &quot;report();&quot; &lt;&lt; std::endl;\n  report();\n  \n  Person *bc = new Person(&quot;Brad Cox&quot;);\n  Person *al = new Person(&quot;Ada Lovelace&quot;);\n  std::cout &lt;&lt; &quot;report();&quot; &lt;&lt; std::endl;\n  report();\n  \n  std::cout &lt;&lt; &quot;deleting Bjarne and Ada&quot; &lt;&lt; std::endl;\n  delete bc;\n  delete al;\n  std::cout &lt;&lt; &quot;report();&quot; &lt;&lt; std::endl;\n  report();\n  \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: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  main.cpp<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Static Members<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/  Created by Bryan Higgs on 8\/23\/24.<\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/<\/span><\/span>\n<span class=\"line\"><\/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 style=\"color: #AF00DB\">#include<\/span><span style=\"color: #0000FF\"> <\/span><span style=\"color: #A31515\">&quot;Person.hpp&quot;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">\/\/ Regular function to report on the current population<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">report<\/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\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;Current population: &quot;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">        &lt;&lt; <\/span><span style=\"color: #267F99\">Person<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">Population<\/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: #267F99\">Person<\/span><span style=\"color: #000000\">::<\/span><span style=\"color: #795E26\">Census<\/span><span style=\"color: #000000\">();<\/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\">void<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">sub<\/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\">  Person <\/span><span style=\"color: #795E26\">bs<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Bjarne Stroustrup&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Person <\/span><span style=\"color: #795E26\">ed<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Edsger Dijkstra&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #795E26\">report<\/span><span style=\"color: #000000\">();<\/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 style=\"color: #0000FF\">int<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argc<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #0000FF\">const<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">char<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">*<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #001080\">argv<\/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\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;report();&quot;<\/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: #795E26\">report<\/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; <\/span><span style=\"color: #A31515\">&quot;sub();&quot;<\/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: #795E26\">sub<\/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; <\/span><span style=\"color: #A31515\">&quot;report();&quot;<\/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: #795E26\">report<\/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\">  Person *bc = <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Person<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Brad Cox&quot;<\/span><span style=\"color: #000000\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  Person *al = <\/span><span style=\"color: #AF00DB\">new<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #795E26\">Person<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #A31515\">&quot;Ada Lovelace&quot;<\/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; <\/span><span style=\"color: #A31515\">&quot;report();&quot;<\/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: #795E26\">report<\/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\">::cout &lt;&lt; <\/span><span style=\"color: #A31515\">&quot;deleting Bjarne and Ada&quot;<\/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\">delete<\/span><span style=\"color: #000000\"> bc;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">  <\/span><span style=\"color: #AF00DB\">delete<\/span><span style=\"color: #000000\"> al;<\/span><\/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;report();&quot;<\/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: #795E26\">report<\/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: #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<p>&#8230; and here is what it outputs:<\/p>\n\n\n\n<pre class=\"wp-block-code\" style=\"padding-top:0px;padding-bottom:0px\"><code>report();\nCurrent population: 0\nCurrent census:\nsub();\nCurrent population: 2\nCurrent census:\nBjarne Stroustrup\nEdsger Dijkstra\nreport();\nCurrent population: 0\nCurrent census:\nreport();\nCurrent population: 2\nCurrent census:\nBrad Cox\nAda Lovelace\ndeleting Bjarne and Ada\nreport();\nCurrent population: 0\nCurrent census:\nProgram ended with exit code: 0<\/code><\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>When used properly:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>static<\/strong> data members <em>can replace global data<\/em><\/li>\n\n\n\n<li><strong>static<\/strong> member functions <em>can replace global functions<\/em><\/li>\n<\/ul>\n\n\n\n<p>All while retaining encapsulation and data abstraction.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong><em>Think about how you might be able to use static class members!<\/em><\/strong> <\/p>\n\n\n\n<p>They can be extremely useful when used properly.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>const, Reference, and static Members const Members C++ has: const Data Members It should be clear that a const data member of a class: This can be useful;&nbsp; it is too seldom used! const Member Functions So, what is a const member function? Here&#8217;s the syntax: where the const is placed after the function argument [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1901,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1157","page","type-page","status-publish","hentry"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1157","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=1157"}],"version-history":[{"count":3,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1157\/revisions"}],"predecessor-version":[{"id":2257,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1157\/revisions\/2257"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/pages\/1901"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalCPlusPlusProgramming\/index.php\/wp-json\/wp\/v2\/media?parent=1157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}