Questions tagged [downcast]

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

589 questions
9
votes
1 answer

Cast Any to Float always fails in swift4.1

In the former version, to get a float value from a [String: Any] dictionary, I can use let float = dict["somekey"] as? Float, but in swift4.1, it doesn't work. It seems the type of dict["somekey"] has been implicitly inferred as Double before I get…
LYM
  • 251
  • 3
  • 9
9
votes
1 answer

implicit upcasting and explicit downcasting in java

When java can implicitly do up casting , why does not it implicitly do down casting ?Please explain with some simple example? …
Number945
  • 4,631
  • 8
  • 45
  • 83
9
votes
4 answers

Could not downcast using List class in Java

I've been searching for an answer for this but to no avail. My question is why is it not possible to downcast with generics. I have a class called Job and extends a class called Model Job extends Model Now I get a collection of Jobs from a reusable…
Strategist
  • 633
  • 1
  • 9
  • 11
9
votes
5 answers

Cast base instance to derived class (downcast) in C#

Suppose I have two classes: class Employee and class AdvancedEmployee:Employee I know something like this won't work, as I can't downcast on C#: var employee = new Employee(); var advanced = employee as AdvancedEmployee; My question is: How to…
Israel Lot
  • 653
  • 1
  • 9
  • 16
8
votes
1 answer

F#: downcast a discriminated union

I have a discriminated union type: type F = | A of int | B of float Suppose I have a list of F that has been filtered to yield only objects of type A: let listOfAs=list.filter (fun f -> match f with | A(f') -> true | _ -> false) How can I work…
Robert Sim
  • 1,428
  • 11
  • 22
8
votes
5 answers

Why do we need Downcasting really?

I am trying to figure out why do I need Downcasting. I reread my notes from collage and found the below example. class Student {...} class Graduate exteds Student { getResearchTopic(){...} // this method only exists in Graduate class. } We…
b4da
  • 3,010
  • 4
  • 27
  • 34
8
votes
2 answers

How to avoid downcast?

I have an implementation of a State Pattern where each state handles events it gets from a event queue. Base State class therefore has a pure virtual method void handleEvent(const Event*). Events inherit base Event class but each event contains its…
Bojan Komazec
  • 9,216
  • 2
  • 41
  • 51
7
votes
4 answers

Downcasting from base pointer to templated derived types

I have the following hierarchy: class base { public: virtual ~base(){} virtual void foo() {} }; template class derived1 : public base { virtual void foo() {}; }; template class derived2 : public base { virtual…
Xander Tulip
  • 1,438
  • 2
  • 17
  • 32
7
votes
6 answers

where downcasting is actually useful?

I know that downcasting is basically casting parent class pointer or reference to the derived class reference or pointer and for that you use dynamic_cast operator. But i can hardly think of any example. could y'all please explain?
Bilal Sheikh
  • 119
  • 7
7
votes
1 answer

Why is a static_cast from a Pointer to Base to a Pointer to Derived "invalid?"

So I have this code: Node* SceneGraph::getFirstNodeWithGroupID(const int groupID) { return static_cast(mTree->getNode(groupID)); } mTree->getNode(groupID) returns a PCSNode*. Node is publicly derived from PCSNode. All of the docs I've…
alk3ovation
  • 1,202
  • 11
  • 15
7
votes
5 answers

How to use dynamic_cast to downcast correctly?

I am being very confused about dynamic_cast. Material from C++ Primer and cppreference(rule 5) can't help me understand. (cppreference is way much harder than the book and I read them both very carefully) From C++ Primer…
Rick
  • 7,007
  • 2
  • 49
  • 79
7
votes
2 answers

Swift error while downcasting 'Any'

The following code is almost exact replica from Apple Documentation and compiles without errors: guard let firstItem = (rawItems! as? Array>)?.first else { throw AnError() } let identityRef =…
cyanide
  • 3,885
  • 3
  • 25
  • 33
7
votes
1 answer

Why isn't automatic downcasting applied to template functions?

Someone asked this question about string appending. It's string s; s = s + 2; not compiling. People gave answers stating that operator+ is defined as a template function while operator+= is not, so auto downcasting (int(2) to char(2)) is not…
iBug
  • 35,554
  • 7
  • 89
  • 134
7
votes
2 answers

How can I take input from either stdin or a file if I cannot seek stdin?

I am porting some Python to Rust as a learning exercise and need to take input from either a file or stdin. I keep a handle to my input in a struct so I thought I'd just make a Box but I ran into a situation where I need to seek on the…
Camden Narzt
  • 2,271
  • 1
  • 23
  • 42
7
votes
2 answers

Java best practice: casting objects vs interfaces

Suppose we have the following toy interfaces: interface Speakable { public abstract void Speak(); } interface Flyer { public abstract void Fly(); } and we have a class that implements both interfaces: class Duck implements Speakable,…
ForeverStudent
  • 2,487
  • 1
  • 14
  • 33
1 2
3
39 40