Questions tagged [upcasting]

Upcasting permits an object of a subclass type to be treated as an object of any superclass type.

244 questions
3
votes
1 answer

Conversion and upcasting

I want to fully understand conversions, i.e. to be sure I know when does a function call would cause an implicit conversion, and when would it cause a compilation error. I've learnt that a conversion may be done if and only if there is a singular…
Idan
  • 5,365
  • 5
  • 24
  • 28
2
votes
2 answers

In F#, what is the simplest method of upcasting a typed list to a seq<_>?

(Newbie question) In F#, what is the simplest method of upcasting a typed list to a seq<_> ? I am working on interop with C# and trying to learn F#. I have specifically a list of movies defined as: Movies: MovieInfo list option which I would like…
Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
2
votes
3 answers

Re-boxing trait objects to generically implemented trait

So far I have rarely had issues with Rust's type inference, but I fear I don't quite understand the problem with the following code: trait SpecificTrait {} struct SpecificStruct; impl SpecificTrait for SpecificStruct {} trait GeneralTrait…
Sty
  • 760
  • 1
  • 9
  • 30
2
votes
1 answer

How to pass unique_ptr by reference polymorphically?

I have a pure virtual class Base and some derived classes ChildA (A/B/C etc): class Base { ... } class ChildA : public Base { ... } I need specific control of the ownership of these child classes, so I spawn them via factory functions and…
2
votes
1 answer

Return a derived object from a generic method

I would like to create a method that can return the generic type defined in the class, here is a detailed example; https://dotnetfiddle.net/SApVp3 using System; public class Program { public static void Main() { …
Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
2
votes
0 answers

Memory layout of multiple inheritance after upcasting

I am studying my c++ lecture, and the professor gives a slide with this code example on. He says that because b1 is the first parent (so I think the first one implemented?), it has the same memory adress as the child class (Mi). Then, I am still…
Jens
  • 141
  • 10
2
votes
1 answer

Dart "upcasting" is not actually upcasting

I am trying to up-cast the subclass object but it is not working. The following program compiles without any errors. VideoStreamModel model = VideoStreamModel(""); VideoStream entity = model; print(model); // prints VideoStreamModel print(entity);…
Nakash Kumar
  • 60
  • 1
  • 7
2
votes
0 answers

Javascript / ES6 Upcast Subclass to Superclass

I'd like to upcast an object of a subclass so that it's properties are exclusively that of its superclass, but I've had no luck finding any ways of going about this in JS, and a surprising lack of documentation on weather or not this is even…
James Oswald
  • 510
  • 3
  • 12
  • 25
2
votes
1 answer

Does the ternary operator combined with C-style upcasting of pointers invoke undefined behavior in this instance?

Motivating background: While trying to debug a hard-to-reproduce fault-condition in my software, I came across some questionable pointer-casting code that was written by a naive junior developer (okay, I admit it, it was me, 10 years ago) that I…
2
votes
3 answers

Java field inheritance and upcasting weird behaviour

This question is inspired by this other one I stumbled upon. Given the following code: public class Test { public static void main(String[] args) { Derived b = new Derived(); Base a = b; System.out.println("b.i -> " +…
beni0888
  • 1,050
  • 1
  • 12
  • 40
2
votes
3 answers

How does upcasting actually works in java

I have 3 classes where one is the super class and other two are sub classes. They have print() method common in them. While calling print() methods from main() I have upcasted the objects to super class then is it not that it must invoke method of…
Vaibhav More
  • 994
  • 3
  • 10
  • 22
2
votes
1 answer

Is it possible to change Kotlin's property getter type?

Is it possible to upcast properties getter type? For example, inside class declaration I have val member = Implementation(), but I want for the public getter to return a reference typed with SomeInterface assuming that Implementation is a subtype of…
nyarian
  • 4,085
  • 1
  • 19
  • 51
2
votes
6 answers

Writing Upcasting and Downcasting expressions in c#

I have been recently studying upcasting and downcasting in c#. I understand that upcasting refers to conversion from a derived class to a base class. However, when i see a practical example of upcasting (like below), i get confused. public class…
Farhan
  • 3,206
  • 14
  • 49
  • 62
2
votes
2 answers

Why an object saves properties when upcasted

I've got a simple question concerning upcasting in c#. For example I have two interfaces: interface IUSer { string UserName { get; set; } } interface IAdmin: IUSer { string Password { get; set; } } ...and a class implementing IAdmin…
Artleks
  • 156
  • 2
  • 16
2
votes
1 answer

Is it possible to upcast to a trait when destructuring enums?

I'd like to be able to destructure/upcast enum variants to a commonly implemented trait. Here's the setup: trait SomeTrait { fn some_func(&self); } struct SomeStruct1; impl SomeTrait for SomeStruct1 { fn some_func(&self) {} } struct…
Ryan C.
  • 87
  • 1
  • 6