Search code examples
c++inheritancevirtualcovariance

Covariant virtual functions return type problem


I have following code:

#include <iostream>
using namespace std;

class Child1
{
    int i;
};

class Child2 : public Child1
{
    int j;
};

class Base1
{

public:

    virtual Child1& getChildren()
    {
        cout << "Children1" << endl;
        return children;
    }

private:

    Child1 children;
};

class Base2 : public Base1
{

public:

    virtual Child2& getChildren()
    {
        cout << "Children2" << endl;
        return children;
    }

private:

    Child2 children;
};

This code compiles fine but when I change the return type of getChildren() from reference type to object type in either or both Base1 and Base2 (e.g. virtual Child2 getChildren(), I get the following error on Visual Studio 2010:

error C2555: 'Base2::getChildren': overriding virtual function return type differs and is not covariant from 'Base1::getChildren'

I want to know why am I not getting this error when using reference and getting it otherwise. Is this is a bug in VS2010? Because the C++ standard (according to this page on Microsoft's website) says something like: 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. And 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.

P.S. I do not have access to the standard at the moment so can not verify the above quote.


Solution

  • You missed the other part they quoted: "If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria: (1) both are pointers to classes or references to classes"