covariance

Getting a vector<Derived*> into a function that expects a vector<Base*>

孤人 提交于 2019-11-26 12:28:33
问题 Consider these classes. class Base { ... }; class Derived : public Base { ... }; this function void BaseFoo( std::vector<Base*>vec ) { ... } And finally my vector std::vector<Derived*>derived; I want to pass derived to function BaseFoo , but the compiler doesn\'t let me. How do I solve this, without copying the whole vector to a std::vector<Base*> ? 回答1: vector<Base*> and vector<Derived*> are unrelated types, so you can't do this. This is explained in the C++ FAQ here. You need to change your

How can I use covariant return types with smart pointers?

点点圈 提交于 2019-11-26 12:27:21
问题 I have code like this: class RetInterface {...} class Ret1: public RetInterface {...} class AInterface { public: virtual boost::shared_ptr<RetInterface> get_r() const = 0; ... }; class A1: public AInterface { public: boost::shared_ptr<Ret1> get_r() const {...} ... }; This code does not compile. In visual studio it raises C2555: overriding virtual function return type differs and is not covariant If I do not use boost::shared_ptr but return raw pointers, the code compiles (I understand this is

C# Covariance on subclass return types

无人久伴 提交于 2019-11-26 09:55:21
问题 Does anyone know why covariant return types are not supported in C#? Even when attempting to use an interface, the compiler complains that it is not allowed. See the following example. class Order { private Guid? _id; private String _productName; private double _price; protected Order(Guid? id, String productName, double price) { _id = id; _productName = productName; _price = price; } protected class Builder : IBuilder<Order> { public Guid? Id { get; set; } public String ProductName { get;

ref and out parameters in C# and cannot be marked as variant

℡╲_俬逩灬. 提交于 2019-11-26 09:37:01
问题 What does the statement mean? From here ref and out parameters in C# and cannot be marked as variant. 1) Does it mean that the following can not be done. public class SomeClass<R, A>: IVariant<R, A> { public virtual R DoSomething( ref A args ) { return null; } } 2) Or does it mean I cannot have the following. public delegate R Reader<out R, in A>(A arg, string s); public static void AssignReadFromPeonMethodToDelegate(ref Reader<object, Peon> pReader) { pReader = ReadFromPeon; } static object

Is this a covariance bug in C# 4?

喜夏-厌秋 提交于 2019-11-26 09:32:56
问题 In the following piece of code I expected to be able to implicitly cast from elements to baseElements because TBase is implicitly convertible to IBase . public interface IBase { } public interface IDerived : IBase { } public class VarianceBug { public void Foo<TBase>() where TBase : IBase { IEnumerable<TBase> elements = null; IEnumerable<IDerived> derivedElements = null; IEnumerable<IBase> baseElements; // works fine baseElements = derivedElements; // error CS0266: Cannot implicitly convert

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism?

天大地大妈咪最大 提交于 2019-11-26 09:06:14
问题 I am trying to figure out the exact meaning of the words Covariance and Contravariance from several articles online and questions on StackOverflow, and from what I can understand, it\'s only another word for polymorphism . Am I correct with the above statement? Or have I got it wrong ? 回答1: It's certainly related to polymorphism. I wouldn't say they're just "another word" for polymorphism though - they're about very specific situations, where you can treat one type as if it were another type

Casting List<T> - covariance/contravariance problem

点点圈 提交于 2019-11-26 09:03:53
问题 Given the following types: public interface IMyClass { } public class MyClass : IMyClass { } I wonder how can I convert a List<MyClass> to a List<IMyClass> ? I am not completely clear on the covariance/contravariance topics, but I understand that I cannot just plainly cast the List because of that. I could come up with this trivial solution only; lacking any elegance, wasting resources: ... public List<IMyClass> ConvertItems(List<MyClass> input) { var result = new List<IMyClass>(input.Count);

Contravariance explained

淺唱寂寞╮ 提交于 2019-11-26 08:58:41
问题 First of, I have read many explanations on SO and blogs about covariance and contravariance and a big thanks goes out to Eric Lippert for producing such a great series on Covariance and Contravariance. However I have a more specific question that I am trying to get my head around a little bit. As far as I understand per Eric\'s explanation is that Covariance and Contravariance are both adjectives that describe a transformation. Covariant transformation is that which preserves the order of

Any simple way to explain why I cannot do List<Animal> animals = new ArrayList<Dog>()? [duplicate]

北战南征 提交于 2019-11-26 08:56:22
问题 This question already has answers here : Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic? (17 answers) Closed 6 years ago . I know why one shouldn\'t do that. But is there way to explain to a layman why this is not possible. You can explain this to a layman easily : Animal animal = new Dog(); . A dog is a kind of animal but a list of dogs is not a list of animals. 回答1: Imagine you create a list of Dogs . You then declare this as List<Animal> and hand

Why cannot IEnumerable<struct> be cast as IEnumerable<object>?

ⅰ亾dé卋堺 提交于 2019-11-26 07:46:58
问题 Why is the last line not allowed? IEnumerable<double> doubleenumerable = new List<double> { 1, 2 }; IEnumerable<string> stringenumerable = new List<string> { \"a\", \"b\" }; IEnumerable<object> objects1 = stringenumerable; // OK IEnumerable<object> objects2 = doubleenumerable; // Not allowed Is this because double is a value type that doesn\'t derive from object, hence the covariance doesn\'t work? Does that mean that there is no way to make this work: public interface IMyInterface<out T> {