Questions tagged [downcast]

Downcasting permits an object of a superclass type to be treated as an object of any subclass type.

589 questions
7
votes
1 answer

Java - downcast in clone

Lets say that Class B extends class A and class A is Cloneable as follows: public class A implements Cloneable { public Object clone() throws CloneNotSupportedException { A ac = (A) super.clone(); return ac; } } public class…
Hesham Yassin
  • 4,341
  • 2
  • 21
  • 23
7
votes
4 answers

Extending a class such that any parent class can be cast to it, in Java

I have a feeling this is impossible, but if not it would be very useful. I’m trying to extend a parent class in a way that the child class only has new methods, no new constructors, no new fields. So the underlying data structure of the child class…
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
7
votes
5 answers

Cost of Up-casting to ArrayList of objects and then down-casting to custom ArrayList

I have a situation in which I am getting data from database, and I want to upcast it to ArrayList of objects and then downcast it to different custom ArrayList i.e. List, List etc. My question is up-casting to object and then…
Androider
  • 2,884
  • 5
  • 28
  • 47
6
votes
2 answers

SW-Design: Adapters for Class Hierarchy in Delphi (Generics vs. Downcast)

I would like to have some suggestions for the following problem: Let's say you want to write adapters for the VCL controls. All Adapters should have the same base class, but differ in wrapping special controls (for example getting a value from a…
Christian Metzler
  • 2,971
  • 5
  • 24
  • 30
6
votes
1 answer

Defining a method for a struct only when a field is a certain enum variant?

I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, …
Addison
  • 3,791
  • 3
  • 28
  • 48
6
votes
2 answers

Why can't the Option.expect() message be downcast as a &'static str when a panic is handled with catch_unwind?

I have the following code: use std::thread; use std::panic; pub fn main(){ thread::spawn(move || { panic::catch_unwind(|| { // panic!("Oh no! A horrible error."); let s: Option = None; …
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
6
votes
2 answers

Downcasting using dynamic_cast returns null

I'm trying to cast a base class object to a derived class object with dynamic_cast, but dynamic_cast returns null. Is it possible to downcast using dynamic_cast? struct A { virtual ~A() {} }; struct B : A {}; int main() { A* a = new A(); …
user3828398
  • 495
  • 3
  • 9
  • 18
6
votes
3 answers

Extend the existing C++ class

I'd like to add the extra functionality without changing the existing class. Say, class base{ public: int i; base(){i = 1;} virtual void do_work(){ /*Do some work*/ } }; If I want to add serialization member function to it, I will…
Mike Jiang
  • 209
  • 1
  • 2
  • 10
6
votes
3 answers

C++ - upcasting and downcasting

In my example: At upcasting, shouldn't the second d.print() call print "base"? Isn't it "d" derived object upcasted to a base class object? And at downcasting, what advantages does it have? Could you explain upcast and downcast in a practical…
Mihai
  • 101
  • 2
  • 2
  • 8
6
votes
1 answer

C++ Avoiding downcasting

I need to parse a source code. I've identified 3 different types of tokens : symbols (operators, keywords), litterals (integers, strings, etc...) and identifiers. I already have the following design, with a base class that keeps track of the type of…
Virus721
  • 8,061
  • 12
  • 67
  • 123
6
votes
5 answers

Is the performance/memory benefit of short nullified by downcasting?

I'm writing a large scale application where I'm trying to conserve as much memory as possible as well as boost performance. As such, when I have a field that I know is only going to have values from 0 - 10 or from -100 - 100, I try to use the short…
asteri
  • 11,402
  • 13
  • 60
  • 84
6
votes
11 answers

Downcasting in C#

I'm facing a problem that I don't know how to solve and am hoping the community can help. I'm writing an app that manages "Lead" objects. (These are sales leads.) One part of my program will import leads from a text file. Now, the text file contains…
The Demigeek
  • 763
  • 3
  • 13
  • 27
5
votes
5 answers

Inheritance and casting for List Objects

I'm having trouble casting a List of Fruit down to the Fruit subclass contained in the List. public class Response { private List mFruitList; public List getFruitList() { return mFruitList; } } public class…
Will Curran
  • 6,959
  • 15
  • 59
  • 92
5
votes
3 answers

Should I avoid downcasting by any means when using factory pattern?

I'm working on a server project that implements a proprietary protocol. The server is implement with factory pattern in C++, and we're now facing the problem of downcasting. The protocol I'm working on is designed for automatic control over slow…
RichardLiu
  • 1,902
  • 1
  • 19
  • 18
5
votes
1 answer

How to downcast with non-polymorphic base class

In C++ , without making my destructor virtual Is it still possible to downcast pointers/references of my non-polymorphic base class?
Adrika
  • 51
  • 3