Downcasting permits an object of a superclass type to be treated as an object of any subclass type.
Questions tagged [downcast]
589 questions
193
votes
3 answers
In Objective-C, what is the equivalent of Java's "instanceof" keyword?
I would like to check whether an object (e.g. someObject) is assignable (cast-able) to a variable of another type (e.g. SpecifiedType). In Java, I can write:
someObject instanceof SpecifiedType
A related question is finding whether the runtime type…

Dimitris
- 682
- 3
- 14
- 19
167
votes
6 answers
Convert base class to derived class
Is it possible in C# to explicitly convert a base class object to one of it's derived classes? Currently thinking I have to create a constructor for my derived classes that accept a base class object as a parameter and copy over the property values.…

ARW
- 3,306
- 7
- 32
- 41
160
votes
9 answers
What is the difference between up-casting and down-casting with respect to class variable
What is the difference between up-casting and down-casting with respect to class variable?
For example in the following program class Animal contains only one method but Dog class contains two methods, then how we cast the Dog variable…

Dhivakar
- 2,081
- 5
- 15
- 18
112
votes
33 answers
Is it possible to assign a base class object to a derived class reference with an explicit typecast?
Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?.
I have tried it and it creates a run-time error.

Maddy.Shik
- 6,609
- 18
- 69
- 98
104
votes
6 answers
downcast and upcast
I am new to C# (and OOP). When I have some code like the following:
class Employee
{
// some code
}
class Manager : Employee
{
//some code
}
Question 1: If I have other code that does this:
Manager mgr = new Manager();
Employee emp…

user184805
- 1,839
- 4
- 19
- 16
103
votes
9 answers
Downcasting optionals in Swift: as? Type, or as! Type?
Given the following in Swift:
var optionalString: String?
let dict = NSDictionary()
What is the practical difference between the following two statements:
optionalString = dict.objectForKey("SomeKey") as? String
vs
optionalString =…

sdduursma
- 2,651
- 3
- 16
- 16
84
votes
3 answers
How does one downcast a std::shared_ptr?
Consider:
struct SomethingThatsABase
{
virtual bool IsChildOne() const { return false; }
virtual bool IsChildTwo() const { return false; }
};
struct ChildOne : public SomethingThatsABase
{
virtual bool IsChildOne() const { return true;…

Billy ONeal
- 104,103
- 58
- 317
- 552
73
votes
7 answers
C++ cannot convert from base A to derived type B via virtual base A
I have four classes:
class A {};
class B : virtual public A {};
class C : virtual public A {};
class D: public B, public C {};
Attempting a static cast from A* to B* I get the below error:
cannot convert from base A to derived type B via virtual…

Panayiotis Karabassis
- 2,278
- 3
- 25
- 40
57
votes
3 answers
When is upcasting illegal in C++?
I am pretty sure I understand the general difference between upcasting and downcasting, particularly in C++. I understand that we can't always downcast because casting a base class pointer to a derived class pointer would assume that the base class…

Connor Foley
- 568
- 4
- 9
53
votes
6 answers
Why can't static_cast be used to down-cast when virtual inheritance is involved?
Consider the following code:
struct Base {};
struct Derived : public virtual Base {};
void f()
{
Base* b = new Derived;
Derived* d = static_cast(b);
}
This is prohibited by the standard ([n3290: 5.2.9/2]) so the code does not…

Eran
- 21,632
- 6
- 56
- 89
40
votes
5 answers
Cast the current object ($this) to a descendent class
I have a class where it may be necessary to change the object to a descendent class further down the line. Is this possible? I know that one option is to return a copy of it but using the child class instead, but it'd be nice to actually modify the…

Nathan MacInnes
- 11,033
- 4
- 35
- 50
40
votes
3 answers
Treating a forced downcast as optional will never produce 'nil'
I've been playing around with Swift and discovered that when down casting an object to be inserted into a dictionary, I get a weird warning: Treating a forced downcast to 'String' as optional will never produce 'nil'. If I replace as with as? then…

Pamelloes
- 690
- 1
- 6
- 13
34
votes
6 answers
Explicit type casting example in Java
I have come across this example on http://www.javabeginner.com/learn-java/java-object-typecasting and in the part where it talks about explicit type casting there is one example which confuses me.
The example:
class Vehicle {
String name;
…

SineLaboreNihil
- 953
- 4
- 11
- 18
29
votes
7 answers
C++ inheritance downcasting
I have my base class as follows:
class point //concrete class
{
... //implementation
}
class subpoint : public point //concrete class
{
... //implementation
}
How do I cast from a point object to a subpoint object? I have tried all…

CodeKingPlusPlus
- 15,383
- 51
- 135
- 216
24
votes
4 answers
How to downcast a Java object?
I am trying to understand Java's polymorphism, and I have one question about downcasting an object.
Let's say for this example I have two subclasses Dog and Cat that inherit from a superclass Animal
From what I understood, the only way to downcast…

nbarraille
- 9,926
- 14
- 65
- 92