Questions tagged [downcast]

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

589 questions
2
votes
2 answers

Downcasting a class c++

I have a doubt about downcasting an object in C++. Here it comes an example: class A { } class B : public A { public: void SetVal(int i) { _v = i; } private: int _v; } A* a = new A(); B* b = dynamic_cast(a); b->SetVal(2); What…
Frion3L
  • 1,502
  • 4
  • 24
  • 34
2
votes
1 answer

What causes MOVE_CAST_ERROR when down-casting a reference object?

I'm creating a class that inherits from a parent class with protected instantiation. The super class has a static method that provides an instance of the class. The following code causes a run-time error MOVE_CAST_ERROR: data: o_child type ref to…
Esti
  • 3,677
  • 8
  • 35
  • 57
2
votes
4 answers

Upcasting in C#

Can we consider value type conversions like int to float conversion as upcasting and float to int as downcasting? I believe when we talk about upcasting and downcasting, we specifically mean reference conversions.
KMH
  • 95
  • 2
  • 10
2
votes
2 answers

downcasting dynamic_cast with non-polymorphic classes, why doesn't it compile?

I have the following code: using namespace std; class BaseOk { public: virtual void ImplementMe() { }; }; class DerivedOk : public BaseOk { public: void ImplementMe() { } }; class CBase { }; class CDerived: public CBase {…
Johnny Pauling
  • 12,701
  • 18
  • 65
  • 108
2
votes
1 answer

Validity of casting a Base pointer to a Derived pointer when Derived only adds methods

First, the question is very similar to downcasting shared pointer to derived class with additional functionality is, where there are good answers. But I'd like to have explanation on why this is valid (or not) and when not using shared pointers. So…
rdrien
  • 65
  • 4
2
votes
6 answers

Java casting error?

Anybody has idea why compiler can't cast value '7' in 'short'? explicit casting is working but while passing parameter it is not working!!! class Alien { String invade(short ships) { return "a few"; } String invade(short... ships) { return…
sHAILU
  • 165
  • 1
  • 10
2
votes
2 answers

Casting 'this' into subclass in superclass constructor

There are two classes: A and B. B is the subclass of A. A stores a reference of B, which may in certain scenarios a reference to this as well. public B b; In the constructor of A, is it legal to cast the this reference to B (provided that I know…
Thomas Calc
  • 2,994
  • 3
  • 30
  • 56
2
votes
7 answers

Force downcasting

I know downcasting is not doable. But I am trying to work around it. This is what I have. public class Ticket{ public int number; public String description; } public class MyTicket extends Ticket{ public String status; } But in my app, I…
dev4life
  • 880
  • 2
  • 8
  • 18
2
votes
2 answers

Base object in constructor as alternative to downcast

I have a list of base objects (RTUDevice) and want to iterate through and convert each to a derived object (actually a derived of a derived RTDSensor) , however the downcasting is throwing an error. public RTUDevice(int id) { _id = id; } public…
PMC
  • 4,698
  • 3
  • 37
  • 57
2
votes
3 answers

How can I avoid downcasting when passing information through a queue?

I'm writing a tool which enables a user to interact with a bit of hardware by changing settings and then streaming information. To do this I have a couple of threads running: EquipmentInterface and DataProcessor which are connected by a Queue. The…
Jon Cage
  • 36,366
  • 38
  • 137
  • 215
1
vote
3 answers

Java downcasting

Hi have one class like this import java.util.ArrayList; public class MobilePhone { private String number; private ArrayList messages; public MobilePhone(String n) { this.number = n; this.messages = new…
Favolas
  • 6,963
  • 29
  • 75
  • 127
1
vote
3 answers

Why Base-to-Derived Dynamic Casting is Only Allowed for Polymorphic Classes

Possible Duplicate: FAQ: Why does dynamic_cast only work if a class has at least 1 virtual method? I have read that in C++, performing a dynamic cast down the hierarchy of a set of classes, the cast is allowed only in a situation where the…
Izza
  • 2,389
  • 8
  • 38
  • 60
1
vote
1 answer

Actionscript 3.0 type downcast issue

I have implemented a new class that extends MovieClip. It's name is base.MovieClipWithDelays ("base" here is a package name). My scene contains such an object named Blah. In Symbol Properties I checked Export for ActionScript and Export in first…
Nick
  • 3,205
  • 9
  • 57
  • 108
1
vote
2 answers

Use Method by Real Type

I learned that I can use the real type of a Object to define which Method is used, such like this: [...] Object foo = new String("hello"); [...] bla(foo); void bla(String x){ } void bla(Integer x){ } Now it should take the bla(String x) method,…
reox
  • 5,036
  • 11
  • 53
  • 98