polymorphism

Returning a derived class of a virtual class in C++

我的梦境 提交于 2021-02-11 12:58:07
问题 here's my problem. I have a template abstract class RandomVariable with pure virtual function operator()() template<T> class RandomVariable<T> { public: virtual T operator()() = 0; /* other stuff */ protected: T value; } I also have a template abstract class Process with pure virtual function operator()() template<T> class Process<T> { public: typedef std::pair<double, T> state; typedef std::list<state> result_type; virtual result_type operator()() = 0; /* other stuff */ protected: result

Why is this invalid Scala?

孤人 提交于 2021-02-11 06:46:26
问题 I'm working with abstract types, and I'm wondering why this is invalid: class A {} class B extends A {} class X {type T = A} class Y extends X {override type T = B} Seeing as B <: A, why can't I assign B to T? I get this error: overriding type T in class X, which equals A; type T has incompatible type class Y extends X {override type T = B} Any help would be appreciated. 回答1: When you say this: class X {type T = A} you say: T is exactly A or T is an alias for A . It can't be anything else,

Why is this invalid Scala?

我只是一个虾纸丫 提交于 2021-02-11 06:46:18
问题 I'm working with abstract types, and I'm wondering why this is invalid: class A {} class B extends A {} class X {type T = A} class Y extends X {override type T = B} Seeing as B <: A, why can't I assign B to T? I get this error: overriding type T in class X, which equals A; type T has incompatible type class Y extends X {override type T = B} Any help would be appreciated. 回答1: When you say this: class X {type T = A} you say: T is exactly A or T is an alias for A . It can't be anything else,

Why is this invalid Scala?

本秂侑毒 提交于 2021-02-11 06:46:17
问题 I'm working with abstract types, and I'm wondering why this is invalid: class A {} class B extends A {} class X {type T = A} class Y extends X {override type T = B} Seeing as B <: A, why can't I assign B to T? I get this error: overriding type T in class X, which equals A; type T has incompatible type class Y extends X {override type T = B} Any help would be appreciated. 回答1: When you say this: class X {type T = A} you say: T is exactly A or T is an alias for A . It can't be anything else,

Can I validate polymorphic XML elements based on a child Type element value?

跟風遠走 提交于 2021-02-10 18:48:14
问题 I want to validate polymorphic Shape elements, differentiated by the Type child element ( not attribute) value. Below are sibling Circle and Rectangle Shape elements. Circles have a Radius and only 1 Point . Rectangles don't have Radius and have 4 Point elements: <?xml version="1.0" encoding="UTF-8" ?> <Shapes> <Shape> <Type>Circle</Type> <ID>A1234</ID> <Label>This is round</Label> <Radius>5.4</Radius> <Points> <Point> <X>5.00</X> <Y>2.00</Y> </Point> </Points> </Shape> <Shape> <Type

Can I validate polymorphic XML elements based on a child Type element value?

我与影子孤独终老i 提交于 2021-02-10 18:47:47
问题 I want to validate polymorphic Shape elements, differentiated by the Type child element ( not attribute) value. Below are sibling Circle and Rectangle Shape elements. Circles have a Radius and only 1 Point . Rectangles don't have Radius and have 4 Point elements: <?xml version="1.0" encoding="UTF-8" ?> <Shapes> <Shape> <Type>Circle</Type> <ID>A1234</ID> <Label>This is round</Label> <Radius>5.4</Radius> <Points> <Point> <X>5.00</X> <Y>2.00</Y> </Point> </Points> </Shape> <Shape> <Type

How to extract information from Java stack as individual data types and use in method call

点点圈 提交于 2021-02-10 18:17:53
问题 I am trying to implement an undo feature by creating a stack of 2 subtypes. I have a stack of parent type UserEntry holding two child types Assign and RelEntry. Assign and RelEntry is both classes used to insert values (number and relationship) into a grid table. There is a method call to insert the values into the table as their respective subtypes for example assignToTable() and RelEntryToTable() . I am trying to use a polymorphic method that can call both of these subtypes from the parent

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

北战南征 提交于 2021-02-10 17:08:14
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

偶尔善良 提交于 2021-02-10 17:05:19
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait

Why can't I cast a Box with an extended trait to a Box with the base trait? [duplicate]

喜你入骨 提交于 2021-02-10 17:04:35
问题 This question already has answers here : Why doesn't Rust support trait object upcasting? (3 answers) Closed 2 years ago . Given the code trait Base { } trait Derived : Base { } struct Foo { } impl Base for Foo { } impl Derived for Foo { } fn main() { let b : Box<Derived> = Box::new( Foo { } ); let a : Box<Base> = b; } When I compile as I'm sure you know I get the following error message: error[E0308]: mismatched types --> src/main.rs:14:25 | 14 | let a : Box<Base> = b; | ^ expected trait