This is called covariance.
In the class hierarchy, when the base class indicates a virtual method that returns either T* or T& , then derived classes are allowed to return U* or U& , respectively, provided that U comes from T (note: and, obviously, combinations of const and volatile )
This is a special rule checked by the compiler, and it works, because if U comes from T , then U* can be attributed to T* . Unfortunately, the rule is limited in that it does not work for any transformation, and therefore even if you can usually build unique_ptr<Shape> from unique_ptr<Rectangle> ... covariance does not work.
That's why, in its Cloneable concept, Boost mandates returns an empty pointer type. This is a shame, but the only way to get covariance.
source share