Downcasting permits an object of a superclass type to be treated as an object of any subclass type.
Questions tagged [downcast]
589 questions
22
votes
1 answer
Why is this value downcast (static_cast to object type) allowed in C++20?
To my surprise, gcc 11.2 accepts this code, but only in C++20 mode:
struct Base {};
struct Derived : Base { int i; };
int f(Base& b) { return static_cast(b).i; }
// ^~~~~~~ oops, forgot the `&`
Likewise,…

ecatmur
- 152,476
- 27
- 293
- 366
20
votes
2 answers
Dynamic downcast on private inheritance within private scope
A tweak on this question that I've run into. Consider:
class A {};
class B : private A {
static void foo();
};
void B::foo(){
B* bPtr1 = new B;
A* aPtr1 = dynamic_cast(bPtr1); // gives pointer
B* bPtr2 = dynamic_cast(aPtr1); //…

Ziv
- 2,369
- 3
- 24
- 40
18
votes
1 answer
Downcast traits inside Rc for AST manipulation
I'm trying to manipulate ASTs in Rust. There will be lots of manipulations, and I want my trees to be immutable, so to save time all references will be Rcs.
My tree nodes will look like this:
enum Condition {
Equals(Rc,…

rix0rrr
- 9,856
- 5
- 45
- 48
17
votes
1 answer
Downcast to derived class in CRTP base class constructor: UB or not?
Consider the following classes:
template
class BaseCRTP {
private:
friend class LinkedList;
Derived *next = nullptr;
public:
static LinkedList instances;
BaseCRTP() {
…

tttapa
- 1,397
- 12
- 26
16
votes
3 answers
Downcasting in Swift with as and as?
What's the difference between these two code snippets:
let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as UITableViewCell?
// vs
let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as? UITableViewCell
Isn't the result…

cfischer
- 24,452
- 37
- 131
- 214
15
votes
1 answer
protocol typed array can't be downcast to concrete type array
protocol P : class {
var value:Int {get}
}
class X : P {
var value = 0
init(_ value:Int) {
self.value = value
}
}
var ps:[P] = [X(1), X(2)]
for p in ps {
if let x = p as? X { // works for a single variable
…

Ken Zhang
- 1,454
- 2
- 13
- 27
14
votes
2 answers
Is this downcasting undefined?
Consider this example, where the base class has some data members, while derived one only provides an additional method:
struct TestBase
{
int x;
TestBase() : x(5) {}
};
struct TestDerived : public TestBase
{
void myMethod()
{
…

Ruslan
- 18,162
- 8
- 67
- 136
13
votes
3 answers
Downcasting shared pointer to derived class with additional functionality - is this safe?
Consider the following outline:
class Base { /* ... */ };
class Derived : public Base
{
public:
void AdditionalFunctionality(int i){ /* ... */ }
};
typedef std::shared_ptr pBase;
typedef std::shared_ptr pDerived;
int…

jedwards
- 29,432
- 3
- 65
- 92
13
votes
4 answers
C++ dynamic_cast - polymorphic requirement and downcasting
In the following code, while constructing obj in case 1, we construct a derived class object too, but its member functions are just inaccessible to obj. So while downcasting (i.e., in case 2), using obj as source, we have the constructed derived in…

Mahesh
- 34,573
- 20
- 89
- 115
11
votes
2 answers
Best way to create a child object from its parent
Which is the best way to create a child given a parent with data?
Would it be ok to have a method with all parents values on the child class as:
public class Child extends Person {
public Child(Parent p) {
…

Molly
- 297
- 1
- 5
- 16
11
votes
3 answers
How do I downcast in python
I have two classes - one which inherits from the other. I want to know how to cast to (or create a new variable of) the sub class. I have searched around a bit and mostly 'downcasting' like this seems to be frowned upon, and there are some…

JPH
- 1,224
- 2
- 14
- 21
11
votes
4 answers
Is it possible to downcast an object to a subclass which does not define extra variable or vtable in C++?
Is it possible to downcast an object to a subclass does not define any extra variable or virtual method?
If I have these classes,
class A { public: A (); };
class B : public A { public: void method1 () {} B (); };
is this (1) possible and (2) safe…

eonil
- 83,476
- 81
- 317
- 516
10
votes
4 answers
Downcasting a list of objects in C#
How can I downcast a list of objects so that each of the objects in the list is downcast to an object of a derived class?
This is the scenario.
I have a base class with a List of base items, and two classes inheriting from it:
public class…

Farinha
- 17,636
- 21
- 64
- 80
10
votes
1 answer
reading from IDbCommand using an inherited custom IDataReader
I have made a custom class inherits IDataReader and have successfully
implemented a custom ServerWriter sqlBulkCopy with the custom class which uses a C# object instead of DataTable.
That proved to be a more efficient approach as I suspected.
Now I…

Jbob Johan
- 221
- 1
- 7
10
votes
2 answers
How can I downcast from Box to a trait object type?
pub struct WidgetWrap {
// ...
widget: RefCell>,
}
At some point I want to cast Box to Box
let mut cell = widget.borrow_mut();
let w = cell.downcast_mut::>();
This gives me an error of this…

porgarmingduod
- 7,668
- 10
- 50
- 83