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 static casting Derived to Base with private inheritance causes a conversion between "const Derived" to "const Base"?
class Base {
};
class Derived: private Base {
};
int main() {
Derived d;
static_cast (d);
}
I understand that such a cast is an error because of private inheritance. However, what I am interested in is why the error message is:
error:…

TYeung
- 2,579
- 2
- 15
- 30
1
vote
2 answers
Interface Downcasting
Please suppose I have the following extension method in order to be able to force evaluation of an IEnumerable:
public static List EvaluateNow(this IEnumerable collection)
{
if (collection == null)
{
throw new…

DotNetStudent
- 889
- 9
- 24
1
vote
0 answers
Java upcasting and downcasting problem as follow
I got confused, as the answer suggested option A.
Shouldn't aVehicle bound be viewed as a Vehicle object since the first line operated upcasting?
The question as follow:
Consider the following code snippet:
Vehicle aVehicle = new…

YI Chen Wang
- 11
- 1
- 2
1
vote
1 answer
How do CLR Enumerables downcast Generics
... and can I do it?
So I wanted to implement an EnumerableSubset class for dealing with pagination results within the API:
public interface IEnumerableSubset : IEnumerable{
int Total {get;}
}
public class ListSubset : List,…

GreysonTyrus
- 687
- 1
- 6
- 15
1
vote
1 answer
Can't downcast from &mut Box as &mut dyn Any?
I encountered this issue, and I'm wondering why it doesn't work.
I managed to replicate the problem :
I'm trying to get back to the struct from &mut Box, and this is my go at it:
use std::any::Any;
trait A {}
struct B;
impl A for B…

LucioleMaléfique
- 476
- 4
- 10
1
vote
2 answers
Is downcasting safe in C++, if the derived class only have new methods(no new variables)?
struct Foo{
Foo(int val) : i(val){}
Foo &operator=(const Foo&){ //... }
void fun(){}
int i;
std::string s;
};
struct Bar : public Foo{
Bar(int i) : Foo(i){}
Bar &operator=(const Bar &other){
…

Theo Mars
- 41
- 4
1
vote
1 answer
Why do we need to downcast a variable even if the function will upcast it again right before returning?
While reading a book about modern C++, I have seen a code snippet that confused me. The code has been written to set up PWM (16 bit Timer) for 8 bit AVR microcontroller. The code is like that:
class pwm_base : private util::noncopyable
{
…

BHOS
- 91
- 7
1
vote
2 answers
Interface downcasting with generics
I have the following code where I want to downcast to an interface with generic but I get Run-time exception: Unable to cast object of type 'FinalAssociator' to type 'IAssociator`1[Common]'.
public interface ICommon
{
string Name…

Ankan Kumar Giri
- 243
- 1
- 3
- 12
1
vote
1 answer
Downcasting pointer to pointer
I'm learning polymorphism in C++ and I can't downcast a pointer to a pointer. I have a class Base and a class Derived that extends Base. And I want to do a pool of Derived objects using a function Base **derivedFactory(size_t size). I tried doing…

acampana
- 461
- 1
- 3
- 17
1
vote
1 answer
Is there a way that i can make my object never null?
I was given a task and I'm not sure how to get it to work. All the test cases must pass.
Assertions.assertDoesNotThrow(() -> p3.equals(null));
Assertions.assertFalse(p3.equals(null), "equals to null should return false");
I figured out the first…

Zaid Fanek
- 23
- 5
1
vote
1 answer
strange behavior of C++ downcasting on object
i ran the code below to assign parent portion of objet to child object.
but as described inline, c style downcast behaves something unexpected.
what happen there? please refer to the comment below.
struct A {
public:
int i{};
…

kile
- 43
- 3
1
vote
1 answer
Java downcasting with two different subclasses
I am learning Java and have been playing with up and downcasting in my command prompt using the classes I defined in Eclipse. I am working with three classes that I included here, and my question is at the bottom:
public class Animal {
public…

Tech320
- 39
- 5
1
vote
2 answers
Type parameters - get concrete type from type T : IMyInterface
Suppose I have a List...
I have three classes which implement IMyInterface: MyClass1, MyClass2, and MyClass3
I have a readonly Dictionary:
private static readonly Dictionary DeclarationTypes = new Dictionary
{
…

Conrad Clark
- 4,533
- 5
- 45
- 70
1
vote
1 answer
Decoding a Codable with class type returned from a function actually returns an item with its superclass as type
I wrote a custom decoder for a json response where I have a "content" field that can be decoded to various different classes, all of them inheriting from the same ContentItem superclass. I also have a function that returns a class type, which I use…

Kappei
- 714
- 2
- 15
- 34
1
vote
2 answers
Downcasting from superclass to several subclasses using Stream
That's my first post and it will be a tough one.
We have a list of Employee class elements, which at the same time can be elements of a 5 types of subclasses: ApprenticeEmployee, SalariedEmployee, CommissionEmployee, CommissionEmployeeWithSalary…

Ketterle
- 15
- 3