I got a question about covariants and overloading. This is my code:
#include <iostream>
using namespace std;
class Parent1
{
};
class Parent2
{
public:
virtual Parent1 * f() { return new Parent1(); }
};
class Child1 : public Parent1
{
public:
class Child2 : public Parent2
{
public:
Child1 * f() { return new Child1(); }
};
};
int main()
{
int x;
cin >> x;
}
This code refuses to compile saying that overload of f() returns
non-covariant of Parent2::f()'s return.
This change compiles fine:
class Child1 : public Parent1
{
public:
};
class Child2 : public Parent2
{
public:
Child1 * f() { return new Child1(); }
};
As does this:
template<class T>
class Child1 : public Parent1
{
public:
class Child2 : public Parent2
{
public:
Child1 * f() { return new Child1(); }
};
};
int main()
{
Child1<int> c;
int x;
cin >> x;
}
I thought that any inner class had access to all names the outer class
does. The outer class knows what it is, why doesn't the inner class
know what the outer class is? Obviously Parent1 is not fully defined
when declaring Child2::f() but what is the rule I'm breaking in the
first example?