Questions tagged [downcast]

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

589 questions
0
votes
1 answer

Pass an implementation of an object without casting

I apologize ahead of time for the title. I am trying to pass an object Cat that implements Animal to an interface called Groom. In my Groom that handles grooming of Cat implementation, I have to downcast my object to understand what I am grooming,…
user0000001
  • 2,092
  • 2
  • 20
  • 48
0
votes
0 answers

Downcasting to protocol with default values in protocol extension

After downcasting an array of structs, my Variables View window shows that all of the values in my struct have shifted "down" (will explain in a second). But when I print(structName), the values are fine. However, when I run an equality check on the…
connorvo
  • 761
  • 2
  • 7
  • 21
0
votes
1 answer

Crash when assigning value

Analytics show 5 users have had 20 crashes when the following method runs. Backend made a change that now returns an array containing a single dictionary. I quickly added this code to cover both cases. What about this code could be causing crashing…
J. Doe
  • 33
  • 7
0
votes
4 answers

Equality check and downcasting code smell in C#

I have an abstract base class Shape with three derived classes: Arc, Circle, and Rectangle. I have implemented an equality check in these classes, and I want to consider Arcs equal to Circles if they have the same shape. To do this, I check the type…
Vahid
  • 5,144
  • 13
  • 70
  • 146
0
votes
4 answers

Why downcasting throws ClassCastException in this case?

I have 2 packages: controller and federation. Controller just contains controller class with main method, but federation contains 2 classes, Shuttle (parent class) and MiniShuttle (child class). Upcasting works like a charm, but when I try to…
Amar Kalabić
  • 888
  • 4
  • 15
  • 33
0
votes
1 answer

How do I get access to getters and setters from a separate class to the main class(with downcasting in C#)

I know how to do this in Java, but C# is not working right. I'm sure I am missing something obvious. I want to downcast Pet to Dog if the Pet is other than a Dog, as you can see. I think the downcast is right, but maybe it is not. The problem is…
inDepth
  • 121
  • 1
  • 13
0
votes
1 answer

Assigning base class to derived and vice versa in C++ and difference between static and dynamic objects

Considering class base { } And class derived: public base {} What's the difference between: FIRST CASE: int main() { base b; derived d; } And SECOND CASE: int main() { base *b; derived *d; } And if we downcast and upcast in the second…
Amine
  • 1,396
  • 4
  • 15
  • 32
0
votes
1 answer

Is downcasting run-time polymorphic?

Upcasting in OOPS makes runtime polymorphism possible by overriding methods of parent and child classes. Can downcasting in OOPS also be run-time polymorphic in any way?
0
votes
2 answers

Downcasting in oops

class Parent {}; class A_child : public Parent { void A_method(); }; class B_child : public Parent { void B_method(); }; void main() { A_child a; Parent *p = &a; B_child *b = (B_child*)&p; b->B_method(); } This piece of code is in C++.…
0
votes
1 answer

Casting Any? to Int, String or Bool

I have a function in Swift that returns Any? value and I would like to cast the result (based on the value type) to Bool, String or Int. The function is this one: static func value(forKey: SettingKeys) -> Any? { return…
Silviu B.
  • 43
  • 8
0
votes
1 answer

Downcasting alternative from a list of super objects

I'm writing a program that keeps track of how many assets a person has. Each person class has an ArrayList of what is called a Value_Item object. Value_Item is a simple object. class Value_Item { float _value; String _currency; String _type…
0
votes
3 answers

Dynamic casting a ** array in c++

I'm trying to make a function that reads a polymorphic dynamic array and prints a different thing for each class. The code is something like this: class A {}; class B: public A {}; class C: public A {}; void function (const A &a) { if(B *b =…
Ramon Pozo
  • 11
  • 2
0
votes
1 answer

C# Downcasting a generic type

interface IMyEvent { } class SpecificEvent : IMyEvent { } interface IMySubscriber where TEvent : IMyEvent { } class MySubscriber : IMySubscriber where TEvent: IMyEvent { } class EventPublisher { public void…
0
votes
0 answers

Is downcasting in this situation acceptable?

I am currently part of a project creating a client-server model to automate a certain task. At a certain point in the project I had to perform downcast to do certain things. Generally, down-casting means your design is flawed. I raised this issue to…
Vino
  • 2,111
  • 4
  • 22
  • 42
0
votes
1 answer

Downcasting in Java with both classes in same file

I am trying to perform an example as told by some faculty but don't seem to understand how this code would work. public class Child extends Base { void show() { System.out.println("Child"); } void display(){ …
Abhishek Dubey
  • 937
  • 8
  • 11