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
How to downcast custom UICollectionViewLayoutAttributes to apply() method?
I just can´t find out how to get access to custom layout attributes in the apply() method of a custom cell.
I have to implement a custom layout attribute to my CollectionViewLayoutAtrributes, so I subclassed them. This works well so far:
class…

HelloTimo
- 558
- 1
- 5
- 18
2
votes
2 answers
Cast an object to child type? (Downcasting)
I understand that downcasting may not be the best way to achieve my desires, but it at least indicates the kind of solutions I have running through my head. Any alternative suggestions are welcome.
Alright, so may as well dive into my code:
public…

K. West
- 31
- 3
2
votes
1 answer
What does it mean to "downcast" a numeric type in python pandas?
From the comments, I'm realizing this question stems from the fact I only code in python, which is dynamically-typed so I've never had to deal with this.
I'm seeing "downcast" used with regard to the altering of a pandas.DataFrame column-type, from…

Rob Truxal
- 5,856
- 4
- 22
- 39
2
votes
4 answers
Is it possible to avoid a downcast?
I have some logic, which defines and uses some user-defined types, like these:
class Word
{
System.Drawing.Font font; //a System type
string text;
}
class Canvass
{
System.Drawing.Graphics graphics; //another, related System type
... and…

ChrisW
- 54,973
- 13
- 116
- 224
2
votes
2 answers
where does downcasted object point to?
public class Animal{
int n = 5;
public static void main(String[] args) {
Animal a = new Animal();
Animal ah = new Horse();
Horse h = new Horse();
System.out.println(h.n); // prints 7
…

javacurious
- 23
- 4
2
votes
2 answers
Swift type casting / downcasting
Why does the Sprite Kit Game template project created by Xcode use as!:
if let sceneNode = scene.rootNode as! GameScene? {...}
Wouldn't the following be equally good?
if let sceneNode = scene.rootNode as? GameScene {...}
Note, this isn't the…

Stephenye
- 806
- 6
- 12
2
votes
4 answers
can you downcast objects in java without declaring a new variable?
I was trying to do something like
class O has a child E
I declare the variable
O xyz = new E();
but then if I call xyz.method(), I can only call those in class O's methods, not E's, so I can downcast it by
E xyz2 = (E) xyz;
My question is- Can…

kamikaze_pilot
- 14,304
- 35
- 111
- 171
2
votes
4 answers
After finding the object type, can I use static_cast to cast the object?
I have written a function that determines which class object is passed, using dynamic_casting.
Inside the condition, can I use static_cast to actually cast the object?
For example, inside someFunc()
class Base
{
public:
Base() { cout <<…

r18ul
- 1,082
- 2
- 12
- 30
2
votes
4 answers
Downcasting or discard polymorphism - what's the smaller evil?
So imagine we have a base class Message:
public abstract Class Message {
Object content;
public Message(Object content) {
this.content = content;
}
}
And various implementations:
public Class Packet extends Message {
…

meow
- 2,062
- 2
- 17
- 27
2
votes
4 answers
How does upcasting works in Java?
I was going with the concept of upcasting and downcasting in java, which are also referred as widening and narrowing.
UpCasting (Widening) happens automatically from derived class to base class. i.e if it has a is-a relationship.
Downcasting has to…

Ritt
- 3,181
- 3
- 22
- 51
2
votes
2 answers
vector returning const reference, impossible to downcast
I am facing an issue in SWIG.
As explained in the doc here, SWIG does not support downcasts in Java and C#. I followed the advices of the doc, and create the correct typemaps for the factory of objects. I can know create an object A and downcast it…

Quentin S.
- 328
- 2
- 16
2
votes
2 answers
Avoid both static & dynamic casting
I'm updating a system that receives multicast messages, parses the data into classes, then passes
a base class pointer to a separate thread through a queue. The other thread then reads the data
from the classes, and stores them in tables.
There are…

Flyboy Wilson
- 49
- 3
2
votes
2 answers
bad_cast exception thrown when doing downcasting from Base class object by reference to derived class object
I still wonder why the following gives std::bad_cast exception
#include
class A {virtual void fun() {}};
class B : public A {};
int main() {
try {
A a;
B b = dynamic_cast(a);
} catch (std::bad_cast& e) {
…

Ibrahim Quraish
- 3,889
- 2
- 31
- 39
2
votes
2 answers
Copying shared variables between derived classes (shallow copy is enough)
So, I have a Base class, and two Derived classes, let's call them Deriv1 and Deriv2.
Let's say Base class has lots of (protected or public) variables, which of course then are also included in Deriv1 and Deriv2.
Now let's say I have an instance of…

kushy
- 356
- 2
- 14
2
votes
1 answer
Accessing a submethod of an argument of an overriden method in android?
This must be a noob question, but I can't find the proper wait to achieve the following:
In android, I made a subclass MyView extending a View class. In B, I've defined a method mMethod not present in the View class.
Now, I want to set an…

Tom
- 1,144
- 1
- 11
- 23