Questions tagged [downcast]

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

589 questions
3
votes
6 answers

Java inheritance downcast ClassCastException

given the following code, I have a question: class A{} class B extends A {} class C extends B{} public class Test { public static void main(String[] args) { A a = new A(); A a1=new A(); B b = new B(); // a=b;//…
Sean Kennedy
  • 63
  • 1
  • 1
  • 6
3
votes
2 answers

Virtual multiple inheritance and casting

I tried creating a class that inherits from multiple classes as followed, getting a "diamond" (D inherits from B and C. B and C both inherits from A virtually):   A   / \ B   C   \ /   D Now, I have a container with…
3
votes
0 answers

Django - Multi-table inheritance - reverse relation to SubClass

I would appreciate your help with following problem. Lets use models from Django documentation to illustrate my situation. models.py from django.db import models class Place(models.Model): owner = DjangoModels.ForeignKey('Owner',…
3
votes
4 answers

Upcast/Downcast and serialization

Just playing around with casting. Assume, we have 2 classes public class Base { public int a; } public class Inh : Base { public int b; } Instantiate both of them Base b1 = new Base {a = 1}; Inh i1 = new Inh {a = 2, b =…
Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64
3
votes
4 answers

Using downcasting in real app life

I know that implicit casting is done automatically by the compiler and virtual machine, and that explicit casting is needed to convert types of data when Java is not sure if the result will be valid. Casting should be done between primitives or…
DRastislav
  • 1,892
  • 3
  • 26
  • 40
3
votes
1 answer

Is it safe to downcast if the derived class contains methods only (no member variables)

Well, I recently ran into the case where I downcasted, by distraction as follows: class Derived: public Base { public: PyObject *GetPyObj() { return m_obj; } void SetPyObj(PyObject *obj) { m_obj = obj } private: PyObject *m_obj; }; This…
Gauthier Boaglio
  • 10,054
  • 5
  • 48
  • 85
3
votes
2 answers

Issues with dynamic_cast from parent to child

I'm working on a basic client server application in C++ using sockets that will run a game of battleship. All communication between client and server is in the form of a simple object hierarchy that looks something like this: namespace Message { …
Jesse Carter
  • 20,062
  • 7
  • 64
  • 101
3
votes
1 answer

Why is DECLARE_DYNAMIC & IMPLEMENT_DYNAMIC nessary for DYNAMIC_DOWNCAST?

I have two classes: /*Switch.h*/ class CSwitch : public CDeviceEntity {} /*EndSystem.h*/ class CEndSystem : public CDeviceEntity {} but when I use: CDeviceEntity* dev = NULL; dev = topo->headList[i]->node; if ( DYNAMIC_DOWNCAST(…
Al2O3
  • 3,103
  • 4
  • 26
  • 52
3
votes
4 answers

Can an upcasted object be downcasted again without trying a cast for every derived class type of the base class type?

I have case where am given a collection of objects that all derive from the same base class. If I iterate over the collection and check each item's type, I can see that the object is of a derived type and then handle it accordingly. What I would…
JWilliams
  • 115
  • 1
  • 9
3
votes
4 answers

How can I downcast to class' type E or at least make it in a safe way without warnings?

I have super abstract class Node and 50 types of subclasses SubNode. I have a generic Class which has a private var List and a method which unfortunately has to accept superclass Node ALWAYS, cannot move it to just E: public…
Whimusical
  • 6,401
  • 11
  • 62
  • 105
3
votes
1 answer

Best practice with dynamic_cast and polymorphism

I have a design problem that I am not sure how to handle in the best way. I want my code to be future proof and still not be to messy and complex (the plight of a geek). Currently my design has the following setup derived class D derived…
MWright
  • 1,681
  • 3
  • 19
  • 31
2
votes
3 answers

Design pattern to avoid downcasting in message passing

Base class MessageHandler has derived classes. They would like to pass messages to each other. Messages could be of different classes, but can be made to share a base class. How can each MessageHandler avoid downcasting a received message? Is it…
2
votes
1 answer

Why casting is not possible from child to parent

I have a class Animal which has one child Dog. class Animal { public void makeSound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override public void makeSound() { super.makeSound(); …
2
votes
1 answer

Does this downcasting lead to undefined behavior?

Given this sample: class Base { public: void foo() {}; }; class Derived : public Base { }; int main() { Base b; Derived* d = static_cast(&b); d->foo(); } I just have three cases:…
user18487620
2
votes
1 answer

dynamic_cast downcasting: How does the runtime check whether Base points to Derived?

I am interested in understanding how, generally speaking, the runtime checks whether a base class actually points to a derived class when using dynamic_cast to apply a downcast. I know that each virtual table of a polymorphic class contains RTTI too…