Covariant Return Types

I ran across something weird the other day. I had assumed that the return type of an overriding function must be identical to the function that it is overriding. The C++ Standard allows one specific exception to this rule that could possibly come in handy to save some unnecessary casting.

In an overridden method that returns a pointer or a reference to a class, you are allowed to change the return type to a derived class instead.

C++ Standard

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. If a function D::f overrides a function B::f, the return types of the functions are covariant if they satisfy the following criteria:

  • both are pointers to classes or references to classes
  • 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
  • both pointers or references have the same cv-qualification and the class type in the return type of D::f has the same cv-qualification as or less cv-qualification than the class type in the return type of B::f.

Example Code

And a quick example that uses this feature:

#include <iostream>

// Just create a class, and a subclass
class Foo {};
class Bar : public Foo {};


class Baz 
{
public:
  virtual Foo * create()
    {
      return new Foo();
    }
};

class Quux : public Baz 
{
public:
  // Different return type, but it's allowed by the standard since Bar
  // is derived from Foo
  virtual Bar * create()
    {
      return new Bar();
    }
};

  

int main() 
{
  Quux *tmp = new Quux();
  Bar *bar = tmp->create();
  
}

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

2 Comments

  1. Uzair:

    This is one of those things that everyone knows but no one uses because either (a) its usefulness never occurred to them or (b) they decide covariant types are too confusing.

    A slightly cleaner way to do the same basic thing is to set up your OO hierarchy correctly, return the base object and then dynamic_cast it to the derived object.

    Still, good post.

  2. tolchz:

    I look at casts as a pretty good indicator of code smell; many casts, especially dynamic, usually indicate that a redesign is in order. This probably causes me to avoid them more then necessary :)

    This could also be one of those nice features of the language that is obscure enough that using a dynamic_cast is less likely to cause someone to waste an hour scratching their head. If you don’t come up with “covariant return type” as a search term, it may take you a while to Google it.

Leave a comment