Suppose I have these abstract classes Foo
and Bar
:
class Foo;
class Bar;
class Foo
{
public:
virtual Bar* bar() = 0;
};
class Bar
{
public:
virtual Foo* foo() = 0;
};
Suppose further that I have the derived class ConcreteFoo
and ConcreteBar
. I want to covariantly refine the return type of the foo()
and bar()
methods like this:
class ConcreteFoo : public Foo
{
public:
ConcreteBar* bar();
};
class ConcreteBar : public Bar
{
public:
ConcreteFoo* foo();
};
This won't compile since our beloved single pass compiler does not know that ConcreteBar
will inherit from Bar
, and so that ConcreteBar
is a perfectly legal covariant return type. Plain forward declaring ConcreteBar
does not work, either, since it does not tell the compiler anything about inheritance.
Is this one of the many shortcomings of C++ I'll have to live with or is there actually a way around this dilemma?