C++ covariance in parameters

The return type is permissible since derived inherits from base, but the function parameter can’t work – not all base instances will be a derived also. What’s supposed to happen in the cases where func is called on a pointer to base with a parameter that’s not a derived? The most derived implementation isn’t callable.

Understanding Covariance and Contravariance in C# 4.0

You may want to look at this blog, he does a fantastic job of explaining it, but I think it will take more examples to clear it up for people, as this gets into a very hard-to-understand area, but, the quote below from the article sums it up well. http://hestia.typepad.com/flatlander/2008/12/c-covariance-and-contravariance-by-example.html “covariance and contravariance” means that … Read more

How to model a covariant association-class in UML?

Preliminary remark: First I’d like to than Bruno and Axel for their respective answers, that put me on the right track. Nevertheless digging further based on their indications, I stumble accross an even simpler solution and supporting references. As it’s useful and following Bruno’s suggestion I’ve finally decided to post it as ananswer. Can simple … Read more

Covariant return type and type conversion

Although Box::duplicate is being invoked at runtime (via virtual dispatch), and although Box::duplicate does override Shape::duplicate (covariantly), and although Box::duplicate does return a Box*, you’ll still get a Shape* pointer because you are calling duplicate() through a Shape* pointer, and Shape* is the return type of Shape::duplicate(), and the compiler only sees you calling Shape::duplicate, … Read more

C++ covariant templates

SmartPtr<Base> and SmartPtr<Derived> are two distinct instantiations of a the SmartPtr template. These new classes do not share the inheritance that Base and Derived do. Hence, your problem. what is the best way to perform this assignment operation? SmartPtr<Base> b = d; Does not invoke assignment operator. This invokes the copy-ctor (the copy is elided … Read more

What is the difference between covariance and contra-variance in programming languages? [closed]

Covariance is pretty simple and best thought of from the perspective of some collection class List. We can parameterize the List class with some type parameter T. That is, our list contains elements of type T for some T. List would be covariant if S is a subtype of T iff List[S] is a subtype … Read more