Downcasting permits an object of a superclass type to be treated as an object of any subclass type.
Questions tagged [downcast]
589 questions
1
vote
1 answer
why the value of ' *ptr ' in the step 3 not equal 3 but 0?
I find that the value of '*ptr' in the steps:3 does not equal 3
#include
using namespace std;
int main(int arg, char* args[])
{
int* ptr;
int x = 4;
float y = 3.142;
cout << y << " " << &y << endl; //step:1
…

Mohamed
- 43
- 1
- 5
1
vote
2 answers
Upcast non-generic ObjC Classes to parent class for Swift method parameter
I have some classes defined in Objective-C similar to this:
@interface Type: NSObject {
}
@end
@interface SubType1: Type {
}
@end
@interface SubType2: Type {
}
@end
@interface Parent : NSObject
@property…

Bogdan
- 402
- 2
- 8
- 18
1
vote
2 answers
Scala: downcasting throws java.lang.ClassCastException
Coming from a non-Java background to Scala has brought me a wide range of difficulties including this one.
scala> class A
defined class A
scala> class B extends A
defined class B
scala> val a = new A
a: A = A@2e893a4a
scala> val b = new…

parsa
- 2,628
- 3
- 34
- 44
1
vote
2 answers
Upcasting/Downcasting to handle mutliple derived classes by base class
Let's say I have a base class and multiple derived classes and I want to avoid having multiple variables/functions for every derived class type.
The only way I found to realize this is using up-/downcasting. But especially downcasting is often…

Odysseus
- 1,213
- 4
- 12
1
vote
1 answer
Can't add viewContoller to Tab Bar programmatically in the AppDelegate
I'm trying to add a viewController to the existing Tab Bar programmatically through didFinishLaunchWithOptions, but my code doesn't work, i can't downcast the rootViewController as UITabBarController.
func application(_ application: UIApplication,…

Khuda
- 13
- 2
1
vote
3 answers
Pointer Upcast and Downcast
Pointer Downcast
int* ptrInt;
char * ptrChar;
void* ptrVoid;
unsigned char indx;
int sample = 0x12345678;
ptrInt = &sample;
ptrVoid = (void *)(ptrInt);
ptrChar = (char *)(ptrVoid);
/*manipulating ptrChar */
…

Amirah Reyna
- 11
- 3
1
vote
1 answer
Downcasting with reinterpret_cast
I am a person, who loves to go deep into nitty-gritty details. This time I created really simple functionality, which I called "Scenario" (look under for code). First I will present to you
my vision of it:
struct ScenarioContext
{ virtual…

Black
- 312
- 2
- 15
1
vote
1 answer
Cannot convert object of base instance to derived type (generics usage)
I have a generic version of an interval with start and end, and a concrete class that uses DateTime.
The generic version defines an Empty value for convenience.
The problem comes when I run the this code.
It complaints in the only line of the Main…

SuperJMN
- 13,110
- 16
- 86
- 185
1
vote
1 answer
draw uml class diagram with both super class object and subclass object being called in a third class
I have a Class A which is the super class of the class B (that is public class B extends A). Now I have a another class C, Class A is a instance variable in class C and class B is being downcasted from Class A and being assign to a local variable in…

Rui
- 117
- 2
- 12
1
vote
1 answer
problem with a HashSet's Iterator
I'm trying to see if HashSet would be the solution for my next project so i'm doing some very easy test to check functionalities.
I have a simple class Klant:
public class Klant {
private int klantNummer;
public Klant(int nummer) {
…

JBoy
- 5,398
- 13
- 61
- 101
1
vote
2 answers
F# Downcasting arrays without reflection
I am working on a program where the user can send me all sort of objects at runtime, and I do not know their type in advance (at compile time). When the object can be down-cast to an (F#) array, of any element type, I would like to perform some…

Janthelme
- 989
- 10
- 23
1
vote
0 answers
What does upcasting and downcasting look at? (Object type or Reference type)
I am a beginner and I am confused between these two examples. So sorry if I ask something that has already be answered. I don't know how to search for this issue.
Let's say I have class Person and class Student. Student extends Person.
So when I…

Stefan Jankovic
- 53
- 4
1
vote
1 answer
How to avoid downcasting when using a list of subclass items in Java
I have an abstract class A and classes B and C. Classes B and C extend A. I need to hold B and C in the list together, say List. However, B and C have unique methods, so not all items in List can be used to invoke certain methods unless I am…

Michael Michael Michael
- 129
- 2
- 7
1
vote
1 answer
JPA: select in polymorphic entities with JPQL, eclipselink and joined inheritance using multiple downcast
I'm having an interesting exercise trying to select multiple derived entities using a single JPQL query while with eclipselink 2.7.6.
The polymorphism is implemented using joined inheritance. The entities diagram and the java classes are as…

el.nicko
- 473
- 6
- 12
1
vote
2 answers
Java ArrayList Downcasting
So I've been working in a project in which I need to have a list to be filled with its new child class objects.
I've made it with traditional method which one ArrayList will be filled with a type of child Class. But to make the code shorter and…

Dean Debrio
- 57
- 1
- 10