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


  • both are pointers to classes or references to classes

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 
  virtual Foo * create()
      return new Foo();

class Quux : public Baz 
  // 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();



  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