covariant

covariant return type

[亡魂溺海] 提交于 2021-02-04 16:27:29
问题 $10.3/5 "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 classes98) — 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 and accessible

Method return type contains subclass definition

会有一股神秘感。 提交于 2020-01-25 03:41:11
问题 I come up with a code that uses a syntax like this: public <A extends B> double[][] foo(C arg) { .... } I get a couple of questions by viewing it. a) The return type of foo(C arg) is <A extends B> double[][] ? What does this mean? I would understood a return type like double[][] for example, but I cannot determine what does the previous modifier (maybe?) <A extends B> does? b) Why there is a subclass declaration inside a return type? Since A is a subclass of B where do we override or add any

Java covariant array bad?

≯℡__Kan透↙ 提交于 2019-12-30 10:33:12
问题 I've been told by several people that Java allows covariant array subtyping in other words if A is a subtype of B, then A[] is a subtype of B[], but that this is a bad feature because it can lead to runtime errors. Can someone give me a concrete example to illustrate how it causes runtime errors and if/how does Java address this problem? Thank you! 回答1: Very simple. String strings[] = {"Broken","Type", "system"}; Object objects[] = strings; objects[0] = 5; // compiles fine, but throws

Invalid covariant type with CRTP clonable class

只愿长相守 提交于 2019-12-18 08:29:24
问题 I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I made this code below, and the compiler shout at me this error: main.cpp:12:5: error: return type of virtual function 'clone' is not covariant with the return type of the function it overrides ('B *' is not derived from 'AbstractClonable *') The class

Invalid covariant type with CRTP clonable class

泄露秘密 提交于 2019-12-18 08:28:18
问题 I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I made this code below, and the compiler shout at me this error: main.cpp:12:5: error: return type of virtual function 'clone' is not covariant with the return type of the function it overrides ('B *' is not derived from 'AbstractClonable *') The class

Why in java method overriding allows to have covariant return types, but not covariant parameters?

余生长醉 提交于 2019-12-18 05:56:09
问题 For example I have a Processor base class with a method that returns an Object and takes Object as a parameter. I want to extend it and create a StringProcessor which will return String and take String as parameter. However covariant typing is only allowed with return value, but not parameter. What is the reason for such limitations? class Processor { Object process (Object input) { //create a copy of input, modify it and return it return copy; } } class StringProcessor extends Processor {

Differing return type for virtual functions

一曲冷凌霜 提交于 2019-12-18 05:47:05
问题 A virtual function's return type should be the same type that is in base class, or covariant. But why do we have this restriction? 回答1: Because of the nonsense that would ensue: struct foo { virtual int get() const { return 0; } }; struct bar : foo { std::string get() const { return "this certainly isn't an int"; } }; int main() { bar b; foo* f = &b; int result = f->get(); // int, right? ...right? } It isn't sensible to have a derived class return something completely unrelated. 回答2: Because

“Invalid covariant return type” errors in nested classes with methods returning template-based objects

懵懂的女人 提交于 2019-12-18 05:05:14
问题 The following C++ code gives me these errors when compiled: covariant.cpp:32:22: error: invalid covariant return type for ‘virtual Q<B> C::test()’ covariant.cpp:22:22: error: overriding ‘virtual Q<A> B::test()’ I do not want to change the line virtual Q<B> test() {} to virtual Q<A> test() {} although it removes the compilation errors. Is there another way to solve this problem? template <class T> class Q { public: Q() {} virtual ~Q() {} }; class A { public: A() {} virtual ~A() {} }; class B {

How covariant method overriding is implemented using bridging Technique in java

主宰稳场 提交于 2019-12-18 04:54:40
问题 While reading on Covariant Overriding, i find out very strange fact, covariant method overriding is implemented using a bridging technique. it also said that this feature is implemented in java5 and above.(i think it is because generics introduced from java5) How it happens.Please help me with example. 回答1: Consider an example: public interface Shape<T extends Shape<T>> { T getType(); void setType(T type); } public class Circle implements Shape<Circle> { Circle getType() { } void setType

Kotlin generics: counterintuitive type inference and checking with out keyword

守給你的承諾、 提交于 2019-12-13 15:05:57
问题 I've been recently learning Kotlin, while having some questions with covariant type. The sample code is here. I have Option and Option2 both having a type parameter T and a run extension. I could understand the first two run in validation() , since they are behaved as Java. But why does the third line compile? Option<T> is invariant in T . We cannot passing Option<C> instance into where Option<B> is expected. After I add an out keyword for T , now all of them could compile. Why? open class A