Downcasting permits an object of a superclass type to be treated as an object of any subclass type.
Questions tagged [downcast]
589 questions
2
votes
1 answer
Downcast list return type in overriden getter of child class
I have the following class hierarchy:
public class U {};
public class IU extends U {};
public class A {
private final List extends U> us;
public A(Collection extends U> us) {
this.us = new ArrayList<>(us);
}
public…

Holt
- 36,600
- 7
- 92
- 139
2
votes
1 answer
What's faster: down-cast from virtual base or cross-cast?
This is somewhat hypothetical as I'm not too worried about performance - just wondering which option is actually the fastest/most efficient in general, or if there is no difference whatsoever.
Suppose I have the following code for a visitor template…

Pete
- 4,784
- 26
- 33
2
votes
0 answers
Ckeditor5 conversion - wrapping image into a link
I am trying to implement a simple ckeditor conversion to wrap my image into an tag.
editor.conversion.for('downcast').add(dispatcher => {
dispatcher.on('insert:imageBlock', (evt, data, conversionApi) => {
const viewImage =…

iNfas
- 43
- 1
- 4
2
votes
1 answer
Trait downcasting
How can I downcast a trait to a struct like in this C# example?
I have a base trait and several derived structs that must be pushed into a single vector of base traits.
I have to check if the each item of the vector is castable to a specific derived…

PrinceOfBorgo
- 508
- 5
- 14
2
votes
1 answer
Handle downcast error when downcasting Ref> into Ref>
I need to write a function foo that takes a &RefCell>, borrows from the RefCell and returns a downcasted object. The downcasted type is chosen at runtime, but for this example let's assume it's usize.
use core::any::Any;
use…

Gabriel Mutti
- 23
- 4
2
votes
2 answers
downcasting in java + calling method with variable arguments
When I call a.displayName("Test") ,it calls the method of class Icecream. displayName(String...s) method takes in variable arguments.
Output-
test Icecream
test Faloodeh
test Faloodeh: Faloodeh
test Faloodeh: Faloodeh
But when I change the…

Mukund Banka
- 66
- 1
- 7
2
votes
1 answer
Is it possible to make a dynamic downcast?
How to downcast with strongly runtime known type ?
public class A {}
public class B : A { public int i; }
public class C
{
B b = new B();
A a = b; // here upcast, and "a" still keeps link to "b"
((B)a).i; //…

noob
- 21
- 2
2
votes
3 answers
Which languages allow to change identity of an object (not cast)?
In this post, a brave wants (in C++) to downcast a object of type Base to a Derived type. Assuming that the Derived type has no more attributes than Base, it can make sense if you're jealous of the extra methods that the Derived class provides.
Are…

CharlesB
- 86,532
- 28
- 194
- 218
2
votes
1 answer
C++ can you downcast class pointer conditionally?
I have a base class (robot) and depending on the arguments passed to the program I want to downcast the robot pointer to a specific derived class. the robot class has virtual functions which are defined in each of the derived classes.
So far I can…

mgw10
- 23
- 4
2
votes
0 answers
Downcasting columns with nullable integers in pandas DataFrames
How can one downcast columns with nullable integers in pandas DataFrames?
# input DataFrame
import pandas as pd, numpy as np
d = pd.DataFrame({'x':[1,3]}, dtype=np.int64)
d['y'] = pd.Series([2,np.nan], dtype=pd.Int64Dtype())
# downcasting 'x'…

S.V
- 2,149
- 2
- 18
- 41
2
votes
1 answer
Why is the compiler allowing me to cast one implementing class to another?
So I was messing around with down-casting, trying to find what works and what doesn't. I have three classes: a base class Animal, and two derived classes Dog and Cat:
private class Animal {
}
private class Dog extends Animal {
}
private class Cat…

nishantc1527
- 376
- 1
- 12
2
votes
2 answers
How can I downcast a pointer after passing it through a function?
I created a function which returns a base pointer to a derived object which is created in the func.
It seems like it doesn't work! As if the derived data is lost, and the pointer is pointing to a base object...
There is class Request, which is the…

Mayuu
- 39
- 7
2
votes
2 answers
Pandas: Read csv with dtypes but mixed type columns(NA values)
Im trying to downcast columns of a csv in the process of reading it, because doing it after reading the file is too time consuming. So far so good. The problem occurs of course if one column has NA values. Is there any possiblity to ignore that or…

Varlor
- 1,421
- 3
- 22
- 46
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
1 answer
Downcasting in Java throws ClassCastException
I don't understand why the casting from bellow doesn't work ( Helicopter h = (Helicopter) new Rotorcraft();) and throws a Runtime exception of type ClassCastException.
Base class:
public class Rotorcraft {
protected final int height = 5;
…

SocketM
- 564
- 1
- 19
- 34