Note that there are some explanatory texts on larger screens.

plurals
  1. POCovariant virtual functions return type problem
    primarykey
    data
    text
    <p>I have following code:</p> <pre><code>#include &lt;iostream&gt; using namespace std; class Child1 { int i; }; class Child2 : public Child1 { int j; }; class Base1 { public: virtual Child1&amp; getChildren() { cout &lt;&lt; "Children1" &lt;&lt; endl; return children; } private: Child1 children; }; class Base2 : public Base1 { public: virtual Child2&amp; getChildren() { cout &lt;&lt; "Children2" &lt;&lt; endl; return children; } private: Child2 children; }; </code></pre> <p>This code compiles fine but when I change the return type of <code>getChildren()</code> from reference type to object type in either or both <code>Base1</code> and <code>Base2</code> (e.g. <code>virtual Child2 getChildren()</code>, I get the following error on Visual Studio 2010:</p> <pre><code>error C2555: 'Base2::getChildren': overriding virtual function return type differs and is not covariant from 'Base1::getChildren' </code></pre> <p>I want to know why am I <strong>not</strong> getting this error when using reference and getting it otherwise. Is this is a bug in VS2010? Because the C++ standard (according to <a href="http://support.microsoft.com/kb/240862" rel="nofollow">this</a> page on Microsoft's website) says something like: <em>The return type of an overriding function shall be either identical to the return type of the overridden function or covariant with the classes of the functions.</em> And <em>the class in the return type of B::f is the same class as the class in the return type of D::f or, is an unambiguous direct or indirect base class of the class in the return type of D::f and is accessible in D.</em></p> <p>P.S. I do not have access to the standard at the moment so can not verify the above quote.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload