Questions tagged [downcast]

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

589 questions
1
vote
1 answer

How to setup C# downcasting in a more dynamic way

Apologies if the title is not quite right. I've got a set of base classes I am deriving from. There is a main one and a bunch of components I can add onto it. public class Entity { public EntityPart ep; } public class EntityPart…
Derek C.
  • 890
  • 8
  • 22
1
vote
3 answers

Overriding the equals() method

class Deneme{ public static void main(String[] args) { Object circle1 = new Circle(); Object circle2 = new Circle(); System.out.println(circle1.equals(circle2)); } } class Circle{ double radius; …
challenGer
  • 13
  • 4
1
vote
3 answers

How can I access gets and sets in a sub class?

In this application we have the Automovel class: public class Automovel { private String marca; private String matricula; private String anoConstrucao; private Motor motor; private int preco = 0; } (with their builders, getters…
1
vote
0 answers

safe downcasting in Apache Spark SQL

We have some well settled source and target data and spark sql in scala is used in between. There are cases when the schema of the target is more restrictive than the one in the source but business says that target schema is more accurate. At this…
Greg Hill
  • 2,148
  • 2
  • 23
  • 27
1
vote
3 answers

Should/how can I avoid downcasting in this case?

Say I have a base and derived class, where the derived class implements some additional manufacture specific functionality: class Device { // Base class } class DeviceFromSpecificManufacture : public Device { // Derived } When my program runs, it…
John O'brien
  • 321
  • 1
  • 9
1
vote
2 answers

is it legal to cast base-instance-pointer to derived-instance-pointer? (the instance is not a derived instance)

I would like to access Base class members in an unqualified way (why? macro sorcery) from outside of the class itself. The strategy is do it in Derived class and cast the pointer-to-Base to pointer-to-Derived (even though the instance is not a…
eudoxos
  • 18,545
  • 10
  • 61
  • 110
1
vote
2 answers

Avoiding downcasting via generics?

Consider the following example in Java. Where I have a method that takes an object of type Dog(which extends Animal), and need to pass it an object of type Animal. I can downcast explicitly but want to avoid this and keep type safety. Is there a way…
1
vote
1 answer

Is there any alternative to dynamic_cast in c++?

I would like to know this in case RTTI is disabled in some compiler environment. Use case: I used this in my code and it worked on my machine but during integration testing the code failed to run properly, rather crashed. I supposed it would have…
Vicky
  • 105
  • 1
  • 2
  • 10
1
vote
1 answer

How to cast nil interface to nil other interface

I have a classic Go nil interface issue. I'm trying to assert an interface{}, which I assign from a nil error, back to an error interface. That sentence is confusing so I have a handy-dandy example: https://play.golang.com/p/Qhv7197oIE_z package…
crunk1
  • 2,560
  • 1
  • 26
  • 32
1
vote
1 answer

How to avoid down-casting when return types are not known at compile time?

Suppose I have a abstract base class called Node. class Node { public: Node() { leftChild = NULL; rightChild = NULL; }; Node * leftChild, *rightChild; void attach(Node * LC, Node * RC) { leftChild = LC; …
Ivor Denham-Dyson
  • 655
  • 1
  • 5
  • 24
1
vote
2 answers

Downcast to template class

I've got template class and I wanted to have vector of this class type. I saw solution where you need to wrap the class with another class without template, and then have a vector of this new class. Now I want to downcast for example one of the list…
CyberGK
  • 47
  • 6
1
vote
1 answer

Downcasting from Any type to UIAccessibilityIdentification always failed

I tried to downcasting from Any type to UIAccessibilityIdentification but always failed let button: Any = UIButton(frame: CGRect.zero) let accessIden = button as? UIAccessibilityIdentification Result always nil. I don't known the reason for…
user2353285
  • 75
  • 1
  • 7
1
vote
2 answers

Swift 4.2 coalescing while downcasting multiple variables in a single if-let

I recently started learning Swift, now I'm trying to safely unwrap multiple variables that come from a JSON response which might contain or not that key. Example of the JSON response: { "products: [{ "foo": "foo" "bar": "bar" …
Frakcool
  • 10,915
  • 9
  • 50
  • 89
1
vote
1 answer

Difference between explicit downcast and upcast

If I have the following situation where I define a class Animal and another class Dog that extends Animal with the following two lines of code: 1) Dog d = (Dog) new Animal(); //explicit downcast 2) Animal a = new Dog(); //automatic upcast What are…
lemonuzzi
  • 21
  • 1
  • 3
1
vote
1 answer

Trying to downcast Object to File in java

i wrote the next piece of code: private ArrayList filter() { ArrayList result = _filters.get(0).buildTree(_dir.listFiles()); for (int i=1; i<_filters.size(); i++){ File[] tempDir = result.toArray(); result =…
yotamoo
  • 5,334
  • 13
  • 49
  • 61