mercredi 11 décembre 2019

Invalid covariant return type on overridden methods with reference return types

I'm implementing inheritence with PIMPL idiom. I have two classes such as :

class Base
{
  virtual BaseImpl& getImpl() const;
  std::unique_ptr<BaseImpl> _pImpl;
};

class Derived : public Base
{
  DerivedImpl& getImpl() const /* override */;
};

class BaseImpl { ... };
class DerivedImpl : public BaseImpl { ... };

Now if I uncomment the override keyword above, compiler complains that the covariant return type is invalid while overriding getImpl().

Since DerivedImpl derives from BaseImpl and I return references, where is the issue with covariance here?

NOTE : This code sample is voluntarily not sorted, classes are in independent files. The issue is only about override keyword. Code compiles without it.

Aucun commentaire:

Enregistrer un commentaire