Downcasting permits an object of a superclass type to be treated as an object of any subclass type.
Questions tagged [downcast]
589 questions
4
votes
1 answer
F# use constructors as functions
I have a use case for treating a constructor for a derived type as a delegate and I can't figure out if it's impossible or I'm just incapable of working it out.
type SomeJobEvent(jobId : int, otherThing : string) =
member this.JobId = jobId
…

tigerswithguitars
- 2,497
- 1
- 31
- 53
4
votes
1 answer
run time error when Casting (down casting) a type to another subType
in numerous other Types I have created it is possible to downCast a type
and i usually Create An Extension method too so it will be easier to manage...
BaseTypeM
BTDerV : BaseTypeM
BTDerLastDescndnt : BTDerV
now i create A LastDerived Type and…

Robb_2015
- 377
- 1
- 3
- 9
4
votes
4 answers
How can I do a safe downcast and prevent a ClassCastException
I have the following scenario:
public class A {
}
public class B extends A {
}
public class C extends B {
public void Foo();
}
I have a method that can return class A, B or C and I want to cast safely to C but only if the class type is C.…

code-gijoe
- 6,949
- 14
- 67
- 103
4
votes
2 answers
JPA downcasting to access subclass methods
DB Table:
Employee Table
ID, Type, Salary, Rate
Type can be full time or contractor. And full time has attribute of salary, contractor has attribute of rate.
(I know it's not normalized. But it's what it is. Appreciate it if you can educate me why…

Weishi Z
- 1,629
- 5
- 18
- 38
4
votes
2 answers
C# casting derived generic type to parent
Before I ask my question this is my structure:
public class Data : ScriptableObject {...}
public class ItemData : Data {...}
public class WeaponData : ItemData {...}
public abstract class Item : Visual where T : ItemData {...}
public class…

Hector Llanos
- 307
- 1
- 3
- 8
4
votes
1 answer
Up casting and down casting confusion in Swift?
class Media {
var name :String = ""
init(name:String) {
self.name = name
}
}
class Song:Media {}
class Movie:Media{}
let s1 = Song(name :"Fireproof")
var m1 :Media = s1 //upcasting
//var s2 :Song = m1
var s2:Song = m1 as Song…

Ali
- 505
- 4
- 12
4
votes
1 answer
C++ Avoiding down-casting or variants
I've been facing a design issue for a while :
I am parsing a source code string into a 1-dimensional array of token objects.
Depending on the type of a token (litteral, symbol, identifier), it has some token-type-specific data. Litterals have a…

Virus721
- 8,061
- 12
- 67
- 123
4
votes
2 answers
Swig downcasting from Base* to Derived*
I have the following c++ classes (simplified) which I am exposing to Python using SWIG:
struct Component
{
virtual void update();
}
struct DerivedComponent : public Component
{
void update() { cout << "DerivedComponent::update()" << endl;…

Homar
- 1,520
- 2
- 17
- 26
4
votes
3 answers
Basic: Connecting multiple (View-)Controllers the right way
I'm trying to set up a login screen (ViewController) that leads - after a successful login - to a user list (UserTableViewController) that is itself part of a navigation controller. On the subsequent screens like the UserTableViewController it…

alexeis
- 2,152
- 4
- 23
- 30
4
votes
2 answers
static_pointer_cast pReallyABase = static_pointer_cast(pBase) works! Why?
I don't understand why this works.
pReallyABase is a downcasted shared_pointer< Derived > which points to a base class instance.
I understand why the compiler lets me call pReallyABase->onlyForDerived() since I defined it as a derived class pointer,…

user3892947
- 41
- 1
4
votes
3 answers
dynamic cast to non-derived object in order to call a function works?
I read in a C++ book that you can use dynamic_cast to downcast a pointer to a base object to a derived object pointer, if the object it points to actually is that of the derived type. The format is dynamic_cast(basePointer). Furthermore, I…

A. Duff
- 4,097
- 7
- 38
- 69
4
votes
4 answers
What actually happens when I perform a downcast?
How exactly does this work?
If I have this base class
public class BaseClass
{
public int Value1 { get; set; }
public int Value2 { get; set; }
public BaseClass SimpleClone()
{
BaseClass result = new BaseClass();
…

Kyle Baran
- 1,793
- 2
- 15
- 30
4
votes
1 answer
Downcast unique_ptr to access a function
How to make it work? Error/comment line before return 0;
#include
#include
#include
using namespace std;
class Base
{
public:
void foobar() { cout << "foobar"; }
};
class Derived : public Base
{
…
user2709465
4
votes
2 answers
why classcastException is not thrown at compile time when downcasted?
Consider the example below. line 5 of main(commented) throws ClassCastException at Runtime.
Line 4 is a valid cast because v1 has "knowledge" of car". In the same token, shouldn't line 5 give compile time error since it has "knowledge of v2" being a…

brain storm
- 30,124
- 69
- 225
- 393
4
votes
1 answer
Safety of invalid downcast using static_cast (or reinterpret_cast) for inheritance without added members
I was wondering what the standard says about the safety of the following code:
class A { int v; };
class B: public A { }; // no added data member
A a;
B& b = static_cast(a);
Obviously the runtime type of a is A, not B, so the cast is not…

Janick Bernet
- 20,544
- 2
- 29
- 55