{"id":6520,"date":"2023-02-26T21:51:26","date_gmt":"2023-02-26T21:51:26","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/covariant-return-type\/"},"modified":"2023-06-26T08:51:45","modified_gmt":"2023-06-26T08:51:45","slug":"covariant-return-type","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/covariant-return-type\/","title":{"rendered":"Covariant Return Type"},"content":{"rendered":"<p>The Covariant Return Type of a member function allows an overriding member function to return a <em>narrower<\/em> type. This is particularly useful when you implement the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Prototype\">Prototype Pattern<\/a>.<\/p>\n<p><!--more--><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6503\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms.png\" alt=\"ClassIdioms\" width=\"650\" height=\"335\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms.png 1224w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-300x154.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-1024x527.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/01\/ClassIdioms-768x395.png 768w\" sizes=\"auto, (max-width: 650px) 100vw, 650px\" \/><\/p>\n<p>I used the Covariant Return Type in my previous posts without explaining it. Let&#8217;s change this.<\/p>\n<h2>Covariant Return Type<\/h2>\n<p>Let me start with the naive implementation.<\/p>\n<h3>The Naive Version<\/h3>\n<p>The following program<code> covariantReturnType.cpp<\/code> applies the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Prototype\">Prototype Pattern<\/a>.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ covariantReturnType.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Window<\/span>{                                  <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n <span style=\"color: #006699; font-weight: bold;\">public<\/span><span style=\"color: #555555;\">:<\/span>                \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> Window<span style=\"color: #555555;\">*<\/span> clone() { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> Window(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"Window\"<\/span>;\r\n    }                       \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Window() {};\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">DefaultWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window {          <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n     DefaultWindow<span style=\"color: #555555;\">*<\/span> clone() override { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> DefaultWindow(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);\r\n    } \r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"DefaultWindow\"<\/span>;\r\n    }   \r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">FancyWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window {           <em><span style=\"color: #0099ff;\">\/\/ (3)<\/span><\/em>\r\n    FancyWindow<span style=\"color: #555555;\">*<\/span> clone() override { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #006699; font-weight: bold;\">new<\/span> FancyWindow(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);\r\n    } \r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"FancyWindow\"<\/span>;\r\n    }   \r\n};\r\n                \r\nWindow<span style=\"color: #555555;\">*<\/span> <span style=\"color: #cc00ff;\">cloneWindow<\/span>(Window<span style=\"color: #555555;\">&amp;<\/span> oldWindow) {    <em><span style=\"color: #0099ff;\">\/\/ (4)<\/span><\/em>                \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> oldWindow.clone();\r\n}\r\n  \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    Window window;\r\n    DefaultWindow defaultWindow;\r\n    FancyWindow fancyWindow;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> window1 <span style=\"color: #555555;\">=<\/span> cloneWindow(window);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"window1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> window1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> defaultWindow1 <span style=\"color: #555555;\">=<\/span> cloneWindow(defaultWindow);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"defaultWindow1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> defaultWindow1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> Window<span style=\"color: #555555;\">*<\/span> fancyWindow1 <span style=\"color: #555555;\">=<\/span> cloneWindow(fancyWindow);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"fancywindow1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> fancyWindow1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> window1;\r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> defaultWindow1;\r\n    <span style=\"color: #006699; font-weight: bold;\">delete<\/span> fancyWindow1;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The interface-class <code>Window<\/code> (line 1) has a virtual <code>clone <\/code>function. The <code>clone<\/code> function returns a copy of itself. The derived classes such as <code>DefaultWindow<\/code> (line 2) and <code>FancyWindow<\/code> (line 3) also return a copy of itself. The function <code>cloneWindow<\/code> (line 4) uses the virtual member function and creates clones of the incoming windows. Additionally, I implemented a virtual <code>getName<\/code> function to visualize the virtual dispatch.<\/p>\n<p>The output of the program is as expected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6517\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnType.png\" alt=\"covariantReturnType\" width=\"493\" height=\"255\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnType.png 493w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnType-300x155.png 300w\" sizes=\"auto, (max-width: 493px) 100vw, 493px\" \/><\/p>\n<p>Did you happen to notice the use of the Covariant Return Type in this example? <code>Window<\/code>&#8216;s virtual clone member function returns a <code>Window<\/code> pointer, but <code>DefaultWindow<\/code>&#8216;s virtual clone member function a <code>DefaultWindow<\/code> pointer, and <code>FancyWindow<\/code>*s virtual clone member function a <code>FancyWindow<\/code> pointer. This means the return type is covariant: <strong> If a derived class member function returns a more-derived type than its overridden base class member function, the derived class return type is called covariant. <\/strong><\/p>\n<p>Additionally, there is a small observation I want to share. Although <code>DeaultWindow<\/code>&#8216;s and <code>FancyWindows<\/code>&#8216;s virtual member functions <code>clone<\/code> are private, the function <code>cloneWindow<\/code> (line 4) can invoke them. The reason is simple: The member function cloneWindow uses the public interface of the interface-class <code>Window<\/code>.<\/p>\n<p>I called this implementation naive. Why?<\/p>\n<h3 id=\"h0-2-ownership-semantics\">Ownership Semantics<\/h3>\n<p>In general, you don&#8217;t know the implementation of the <code>clone<\/code> function. <code>clone<\/code> returns a pointer to a window<code><\/code>. <strong>Pointers have, by design, a flaw. They model two completely different semantics: ownership and borrowing.<\/strong><\/p>\n<ul>\n<li><strong>Ownership<\/strong>: the caller is responsible for the windows and must destroy them; this is the behavior that the program <code>covariantReturnType.cpp<\/code> modeled<\/li>\n<li><strong>Borrowing<\/strong>: the callee is responsible for the Window and borrows it from the caller<\/li>\n<\/ul>\n<p>Let me emphasize this one more.<\/p>\n<ul>\n<li><strong>Owner<\/strong>: You are the owner of the w<code>indow<\/code>. You have to take care of it and destroy it. If not, you have a memory leak.<\/li>\n<li><strong>Borrower<\/strong>: You are not the owner of the window. You can not destroy it. If you destroy it, you have a double delete.<\/li>\n<\/ul>\n<p>With C++11, it is easy to overcome this pointer flaw. Use a<code> std::unique_ptr&lt;Window&gt;<\/code> or a <code>std::shared_ptr&lt;Window&gt;.&nbsp; <\/code><\/p>\n<\/p>\n<h3><code>std::unique_ptr<\/code><\/h3>\n<p>Returning&nbsp;<code> std::unique_ptr&lt;Window&gt;<\/code> means that the caller is the owner. The<code> std::unique_ptr&lt;Window&gt;<\/code> behaves as a local. When it goes out of scope, it is automatically destroyed. Additionally, you can simulate the Covariant Return Type.<\/p>\n<p>Thanks to the remark from<a href=\"https:\/\/www.modernescpp.com\/index.php\/covariant-return-type?fbclid=IwAR0EEZrYZVA6PTjJpnq2LjNlsXc9U9L3wWY6FjUwoXflHjmLgp2dMlgj4dQ\"> Alf P. Steinbach on the facebook group C++ Enthusiasts<\/a>, I want to clarify. Both examples using the smart pointers<code> std::unique_ptr<\/code> and<code> std::shared_ptr<\/code> do not use a covariant return type. The smart pointers have different dynamic types. This is, in this use-case, sufficient to simulate the behavior of the first program<code> covariantReturntype.cpp<\/code>.&nbsp;<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0px; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ covariantReturnTypeUniquePtr.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;memory&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Window<\/span>{ \r\n <span style=\"color: #006699; font-weight: bold;\">public<\/span><span style=\"color: #555555;\">:<\/span>                \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> clone() { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);          <em><span style=\"color: #0099ff;\">\/\/ (1)<\/span><\/em>\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"Window\"<\/span>;\r\n    }                       \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Window() {};\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">DefaultWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window { \r\n     std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> clone() override { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>DefaultWindow<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);  <em><span style=\"color: #0099ff;\">\/\/ (2)<\/span><\/em>\r\n    } \r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"DefaultWindow\"<\/span>;\r\n    }   \r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">FancyWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window { \r\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> clone() override { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>FancyWindow<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);  <em><span style=\"color: #0099ff;\"> \/\/ (3)<\/span><\/em>\r\n    } \r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"FancyWindow\"<\/span>;\r\n    }   \r\n};\r\n                \r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> <span style=\"color: #cc00ff;\">cloneWindow<\/span>(std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;&amp;<\/span> oldWindow) {                    \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> oldWindow<span style=\"color: #555555;\">-&gt;<\/span>clone();\r\n}\r\n  \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> window <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span>();\r\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> defaultWindow <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>DefaultWindow<span style=\"color: #555555;\">&gt;<\/span>();\r\n    std<span style=\"color: #555555;\">::<\/span>unique_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> fancyWindow <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_unique<span style=\"color: #555555;\">&lt;<\/span>FancyWindow<span style=\"color: #555555;\">&gt;<\/span>();\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> window1 <span style=\"color: #555555;\">=<\/span> cloneWindow(window);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"window1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> window1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> defaultWindow1 <span style=\"color: #555555;\">=<\/span> cloneWindow(defaultWindow);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"defaultWindow1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> defaultWindow1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> fancyWindow1 <span style=\"color: #555555;\">=<\/span> cloneWindow(fancyWindow);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"fancyWindow1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> fancyWindow1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The virtual <code>clone<\/code> member function&#8217;s return type is a <code>std::unique_ptr&lt;Window&gt;<\/code>. Additionally,&nbsp; the returned object is a <code>std::make_unique&lt;Window&gt;(*this)<\/code> (line 1), a <code>std::make_unique&lt;DefaultWindow&gt;(*this)<\/code> (line 2), or a <code>std::make_unique&lt;FancyWindow&gt;(*this)<\/code> (line 3), respectively.<\/p>\n<p>The output of the program is identical to the previous one.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6518\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnTypeUniquePtr.png\" alt=\"covariantReturnTypeUniquePtr\" width=\"520\" height=\"254\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnTypeUniquePtr.png 520w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnTypeUniquePtr-300x147.png 300w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/p>\n<div>\n<div>\n<div>\n<div>&nbsp;<\/div>\n<div>For completeness, let me implement this example using a <code>std::shared_ptr<\/code>.<\/div>\n<\/div>\n<\/div>\n<\/div>\n<h3><code>std::shared_ptr<\/code><\/h3>\n<p>Returning a<code> std::shared_ptr&lt;Window&gt;<\/code> means that the caller and the called share ownership. When neither the caller nor the callee needs the<code> std::shared_ptr&lt;Window&gt;<\/code> anymore, it is automatically destroyed.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ covariantReturnTypeSharedPtr.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;memory&gt;<\/span>\r\n<span style=\"color: #009999;\">#include &lt;string&gt;<\/span>\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">Window<\/span>{ \r\n <span style=\"color: #006699; font-weight: bold;\">public<\/span><span style=\"color: #555555;\">:<\/span>                \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> clone() { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_shared<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);\r\n    }\r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"Window\"<\/span>;\r\n    }                       \r\n    <span style=\"color: #006699; font-weight: bold;\">virtual<\/span> <span style=\"color: #555555;\">~<\/span>Window() {};\r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">DefaultWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window { \r\n     std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> clone() override { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_shared<span style=\"color: #555555;\">&lt;<\/span>DefaultWindow<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);\r\n    } \r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"DefaultWindow\"<\/span>;\r\n    }   \r\n};\r\n\r\n<span style=\"color: #006699; font-weight: bold;\">class<\/span> <span style=\"color: #00aa88; font-weight: bold;\">FancyWindow<\/span><span style=\"color: #555555;\">:<\/span> <span style=\"color: #006699; font-weight: bold;\">public<\/span> Window { \r\n    std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> clone() override { \r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> std<span style=\"color: #555555;\">::<\/span>make_shared<span style=\"color: #555555;\">&lt;<\/span>FancyWindow<span style=\"color: #555555;\">&gt;<\/span>(<span style=\"color: #555555;\">*<\/span><span style=\"color: #006699; font-weight: bold;\">this<\/span>);\r\n    } \r\n    std<span style=\"color: #555555;\">::<\/span>string getName() <span style=\"color: #006699; font-weight: bold;\">const<\/span> override {\r\n        <span style=\"color: #006699; font-weight: bold;\">return<\/span> <span style=\"color: #cc3300;\">\"FancyWindow\"<\/span>;\r\n    }   \r\n};\r\n                \r\n<span style=\"color: #006699; font-weight: bold;\">auto<\/span> <span style=\"color: #cc00ff;\">cloneWindow<\/span>(std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;&amp;<\/span> oldWindow) {                    \r\n    <span style=\"color: #006699; font-weight: bold;\">return<\/span> oldWindow<span style=\"color: #555555;\">-&gt;<\/span>clone();\r\n}\r\n  \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() {\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> window <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_shared<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span>();\r\n    std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> defaultWindow <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_shared<span style=\"color: #555555;\">&lt;<\/span>DefaultWindow<span style=\"color: #555555;\">&gt;<\/span>();\r\n    std<span style=\"color: #555555;\">::<\/span>shared_ptr<span style=\"color: #555555;\">&lt;<\/span>Window<span style=\"color: #555555;\">&gt;<\/span> fancyWindow <span style=\"color: #555555;\">=<\/span> std<span style=\"color: #555555;\">::<\/span>make_shared<span style=\"color: #555555;\">&lt;<\/span>FancyWindow<span style=\"color: #555555;\">&gt;<\/span>();\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> window1 <span style=\"color: #555555;\">=<\/span> cloneWindow(window);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"window1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> window1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> defaultWindow1 <span style=\"color: #555555;\">=<\/span> cloneWindow(defaultWindow);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"defaultWindow1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> defaultWindow1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #006699; font-weight: bold;\">auto<\/span> fancyWindow1 <span style=\"color: #555555;\">=<\/span> cloneWindow(fancyWindow);\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"fancyWindow1-&gt;getName(): \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> fancyWindow1<span style=\"color: #555555;\">-&gt;<\/span>getName() <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">'\\n'<\/span>;\r\n  \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Porting the example <code>covariantReturnTypeUniquePtr.cpp<\/code> into <code>covariantReturnTypeSharedPtr.cpp<\/code> is a piece of cake: I replaced unique with shared. Without further ado, here is the output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-6519\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnTypeSharedPtr.png\" alt=\"covariantReturnTypeSharedPtr\" width=\"520\" height=\"254\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnTypeSharedPtr.png 520w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2023\/02\/covariantReturnTypeSharedPtr-300x147.png 300w\" sizes=\"auto, (max-width: 520px) 100vw, 520px\" \/><\/p>\n<p><strong>Thanks to the remark from<a href=\"https:\/\/www.modernescpp.com\/index.php\/covariant-return-type?fbclid=IwAR0EEZrYZVA6PTjJpnq2LjNlsXc9U9L3wWY6FjUwoXflHjmLgp2dMlgj4dQ\"> Alf P. Steinbach on the facebook group C++ Enthusiasts<\/a>, I want to clarify. Both examples using the smart pointers<code> std::unique_ptr<\/code> and<code> std::shared_ptr<\/code> do not apply a covariant return type. The smart pointers have different dynamic types. This is, in this use case, sufficient to simulate the behavior of the first program<code> covariantReturntype.cpp<\/code>.&nbsp;<\/strong><\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>My next post is special. I will give an overview of posts I have written about idioms dealing with polymorphism and templates.<\/p>\n<p>&nbsp;<\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Covariant Return Type of a member function allows an overriding member function to return a narrower type. This is particularly useful when you implement the Prototype Pattern.<\/p>\n","protected":false},"author":21,"featured_media":6503,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[379],"tags":[400,399,401],"class_list":["post-6520","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-patterns","tag-shared_ptr","tag-smart-pointers","tag-unique_ptr"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6520","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=6520"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6520\/revisions"}],"predecessor-version":[{"id":6653,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/6520\/revisions\/6653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/6503"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=6520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=6520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=6520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}