{"id":5013,"date":"2016-11-03T21:53:07","date_gmt":"2016-11-03T21:53:07","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/override-and-final\/"},"modified":"2023-06-26T12:37:29","modified_gmt":"2023-06-26T12:37:29","slug":"override-and-final","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/override-and-final\/","title":{"rendered":"override and final"},"content":{"rendered":"<p>Using the context-sensitive keyword override and final, you can explicitly manage the overriding of virtual functions. In particular, the keyword override solves many issues with difficulty finding bugs in object hierarchies: Methods that should override methods of base classes. The result is syntactically but not a semantically correct program. The program performs the wrong stuff in the right way.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<h2>override<\/h2>\n<p>To override a method, the signature of the overriding method has to match precisely. What sounds easy is often not easy in practice: If the method&#8217;s signature fits not precisely, you will get the correct program with the wrong behavior. That&#8217;s simple; a different method will be invoked.<\/p>\n<p>The name <span style=\"font-family: courier new,courier;\">override<\/span> in the method declaration expresses that the method should override a virtual method of a base class. The compiler checks the assertion. It checks the parameter of the method, the return type of the method, and qualifiers like <span style=\"font-family: courier new,courier;\">const<\/span> and <span style=\"font-family: courier new,courier;\">volatile.<\/span> Of course, the compiler notices if the <em>overridden<\/em> method is not virtual.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30\r\n31\r\n32\r\n33<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ override.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Base<\/span> {\r\n\r\n  <span style=\"color: #2b91af;\">void<\/span> func1();\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> func2(<span style=\"color: #2b91af;\">float<\/span>);\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> func3() <span style=\"color: #0000ff;\">const<\/span>;\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">long<\/span> func4(<span style=\"color: #2b91af;\">int<\/span>);\r\n\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> f();\r\n\r\n};\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Derived<\/span>: <span style=\"color: #0000ff;\">public<\/span> Base {\r\n\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> func1() override;\r\n\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> func2(<span style=\"color: #2b91af;\">double<\/span>) override;\r\n\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> func3() override;\r\n\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">int<\/span> func4(<span style=\"color: #2b91af;\">int<\/span>) override;\r\n\r\n  <span style=\"color: #2b91af;\">void<\/span> f() override;\r\n\r\n};\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  Base base;\r\n  Derived derived;\r\n\r\n};\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>If you compile the program, the compiler will complain a lot. The error message is very specific.<\/p>\n<p>The compiler complains in line 16 that the method <span style=\"font-family: courier new,courier;\">func1<\/span> is not overriding a method. The same holds for the <span style=\"font-family: courier new,courier;\">func2.<\/span> It has the wrong parameter type. It goes on with the method <span style=\"font-family: courier new,courier;\">func3. func3<\/span> has no <span style=\"font-family: courier new,courier;\">const<\/span> qualifier. <span style=\"font-family: courier new,courier;\">func4<\/span> has the wrong return type. Only method <span style=\"font-family: courier new,courier;\">f<\/span> in line 24 did it right and overrides the method <span style=\"font-family: courier new,courier;\">f<\/span> of their base class.<\/p>\n<p>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5010\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/override.png\" alt=\"override\" width=\"700\" height=\"291\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/override.png 874w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/override-300x125.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/override-768x320.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>It&#8217;s a job for <span style=\"font-family: courier new,courier;\">final<\/span> If a virtual method should not be overridden.<\/p>\n<\/p>\n<h2>final<\/h2>\n<p><span style=\"font-family: courier new,courier;\">final<\/span> supports two use cases. First, you can declare a method that can not be overridden; second, you can define a class from which you can not derive. The compiler uses the same rules as in the case of <span style=\"font-family: courier new,courier;\">override<\/span> to determine if a method overrides a method of a base class. Of course, the strategy goes the other way around because the final should suppress the overriding of a method. Therefore, the compiler checks the parameter of the method, their return type, and the <span style=\"font-family: courier new,courier;\">const\/volatile<\/span> qualifiers.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19\r\n20\r\n21\r\n22\r\n23\r\n24\r\n25\r\n26\r\n27\r\n28\r\n29\r\n30<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ final.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Base<\/span> {\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> h(<span style=\"color: #2b91af;\">int<\/span>) final;\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> g(<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">int<\/span>);\r\n};\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">Derived<\/span>: <span style=\"color: #0000ff;\">public<\/span> Base {\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> h(<span style=\"color: #2b91af;\">int<\/span>);\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> h(<span style=\"color: #2b91af;\">double<\/span>);\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> g(<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">int<\/span>) final;\r\n};\r\n\r\n<span style=\"color: #0000ff;\">class<\/span> <span style=\"color: #2b91af;\">DerivedDerived<\/span>: <span style=\"color: #0000ff;\">public<\/span> Derived {\r\n  <span style=\"color: #0000ff;\">virtual<\/span> <span style=\"color: #2b91af;\">void<\/span> g(<span style=\"color: #2b91af;\">long<\/span> <span style=\"color: #2b91af;\">int<\/span>);\r\n};\r\n\r\n<span style=\"color: #0000ff;\">struct<\/span> FinalClass final { };\r\n<span style=\"color: #0000ff;\">struct<\/span> DerivedClass: FinalClass { };\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  Base base;\r\n  Derived derived;\r\n  DerivedDerived derivedDerived;\r\n\r\n  FinalClass finalClass;\r\n  DerivedClass derivedClass;\r\n\r\n};\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>What&#8217;s happening at compile time?<\/p>\n<p>The compiler does its job very neatly. It complains that the method <span style=\"font-family: courier new,courier;\">h<\/span> in the class <span style=\"font-family: courier new,courier;\">Base<\/span> (line 4) is overridden by the method in class <span style=\"font-family: courier new,courier;\">Derived<\/span> (line 9). Of course, it&#8217;s okay that the method <span style=\"font-family: courier new,courier;\">h<\/span> (line 10) in class <span style=\"font-family: courier new,courier;\">Derived<\/span> overloads f for the parameter type <span style=\"font-family: courier new,courier;\">double.<\/span> The method g (line 11) in the class Derived is quite interesting<span style=\"font-family: courier new,courier;\">.<\/span> The method overrides method <span style=\"font-family: courier new,courier;\">g<\/span> (line 5) of the class <span style=\"font-family: courier new,courier;\">Base<\/span> and declares the method <span style=\"font-family: courier new,courier;\">final.<\/span> Therefore, <span style=\"font-family: courier new,courier;\">g<\/span> can not be overridden in <span style=\"font-family: courier new,courier;\">DerivedDerived<\/span> (line 15).<\/p>\n<p>To the class <span style=\"font-family: courier new,courier;\">FinalClass<\/span> (line 18). <span style=\"font-family: courier new,courier;\">DerivedClass<\/span> can not be derived from <span style=\"font-family: courier new,courier;\">FinalClass,<\/span> because the <span style=\"font-family: courier new,courier;\">BaseClass<\/span> is final.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5011\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/final.png\" alt=\"final\" width=\"700\" height=\"292\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/final.png 874w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/final-300x125.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/final-768x320.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>I intentionally ignored one fact. The keywords <span style=\"font-family: courier new,courier;\">override<\/span> and <span style=\"font-family: courier new,courier;\">final<\/span> are<strong> context-sensitive keywords<\/strong>. What does that mean?<\/p>\n<h2>Context-sensitive keywords<\/h2>\n<p><span style=\"font-family: courier new,courier;\">override<\/span> and <span style=\"font-family: courier new,courier;\">final<\/span> are only keywords in specific contexts. These contexts are the declaration of a method or a class. If you use them in other contexts, they will be identifiers. What was the reason for introducing context-sensitive keywords into the C++ standard? On the one hand, the C++ standardization committee doesn&#8217;t like it introducing new keywords; on the other hand, the classical programs keep valid if they use the context-sensitive keywords <span style=\"font-family: courier new,courier;\">override<\/span> and <span style=\"font-family: courier new,courier;\">final.<\/span> With classical programs, I mean a program written with C++98\/C++03 syntax in mind.<\/p>\n<p>Context-sensitive keywords following a key principle of C++: <strong>Don&#8217;t break existing code.<\/strong><\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<table>\n<tbody>\n<tr>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"> 1\r\n 2\r\n 3\r\n 4\r\n 5\r\n 6\r\n 7\r\n 8\r\n 9\r\n10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18<\/pre>\n<\/td>\n<td>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000;\">\/\/ keywords.cpp<\/span>\r\n\r\n<span style=\"color: #0000ff;\">#include &lt;iostream&gt;<\/span>\r\n\r\n<span style=\"color: #2b91af;\">void<\/span> override(){ std::cout &lt;&lt; <span style=\"color: #a31515;\">\"override\"<\/span> &lt;&lt; std::endl; }\r\n\r\n<span style=\"color: #2b91af;\">int<\/span> main(){\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n  override();\r\n  \r\n  <span style=\"color: #0000ff;\">auto<\/span> final(2011);\r\n  std::cout &lt;&lt; <span style=\"color: #a31515;\">\"final: \"<\/span> &lt;&lt; final &lt;&lt; std::endl;\r\n\r\n  std::cout &lt;&lt; std::endl;\r\n\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>&nbsp;<\/p>\n<p>My small program is valid C++, although I named the function <span style=\"font-family: courier new,courier;\">override<\/span> (line 5) and gave the variable the name <span style=\"font-family: courier new,courier;\">final<\/span> (line 13).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5012\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/keywords.png\" alt=\"keywords\" width=\"400\" height=\"184\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/keywords.png 425w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2016\/11\/keywords-300x138.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Only for the sake of completeness. C++11 has with <span style=\"font-family: courier new,courier;\">default<\/span> and <span style=\"font-family: courier new,courier;\">delete<\/span> additional context-sensitive keywords.<\/p>\n<h2>What&#8217;s next?<\/h2>\n<p>The new keyword <span style=\"font-family: courier new,courier;\">nullptr<\/span> defines a null pointer constant in C++11. <span style=\"font-family: courier new,courier;\">nullptr<\/span> clears the ambiguity of the number 0 in C++ and the&nbsp;macro NULL in C. How? You have to wait for the <a href=\"https:\/\/www.modernescpp.com\/index.php\/the-null-pointer-constant-nullptr\">next post. <\/a><span style=\"font-family: courier new,courier;\"><a href=\"https:\/\/www.modernescpp.com\/index.php\/the-null-pointer-constant-nullptr\"><\/a><span id=\"transmark\"><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using the context-sensitive keyword override and final, you can explicitly manage the overriding of virtual functions. In particular, the keyword override solves many issues with difficulty finding bugs in object hierarchies: Methods that should override methods of base classes. The result is syntactically but not a semantically correct program. The program performs the wrong stuff [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":5010,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[364],"tags":[515,516],"class_list":["post-5013","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded","tag-final","tag-override"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5013","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5013"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5013\/revisions"}],"predecessor-version":[{"id":6930,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5013\/revisions\/6930"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5010"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5013"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5013"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5013"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}