This question was asked here a few hours ago and made me realise that I have never actually used covariant return types in my own code. For those not sure what covariance is
I think covariance can be useful when declaring factory methods that return a specific class and not its base class. This article explains this scenario quite well, and includes the following code example:
class product
{
...
};
class factory
{
public:
virtual product *create() const = 0;
...
};
class concrete_product : public product
{
...
};
class concrete_factory : public factory
{
public:
virtual concrete_product *create() const
{
return new concrete_product;
}
...
};