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

Java downcast objects invoking sub method

The printout is: Fruit Apple Golden Golden I want to know why the object c.make() invokes a method in class Golden instead of class Apple.Because I think that c is the object of class Apple, where…
1
vote
1 answer

Avoiding downcasts in a Swift 3 completion handler with Google Drive REST API

I'm using the Google Drive REST API in a Swift 3 app. Queries are executed using the executeQuery method of GTLRDriveService. This method takes a completion block of type GTLRServiceCompletionHandler?, which in turn is declared as public typealias…
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1
vote
1 answer

Derived class has no extra data members; Is it safe to statically downcast a base object to a derived object?

struct Base { int i, j; }; struct Derived : Base {}; With above scenario, if we do following: Base b; auto& d = static_cast(b); d.i = 1; Will it be an undefined behaviour? Note: For some reasons, I can't edit the code of an auto…
iammilind
  • 68,093
  • 33
  • 169
  • 336
1
vote
2 answers

use `static_cast` for downcasting over a null pointer (single or multiple inheritance)

As the title says, if I cast a pointer to a base class, to a derived class, when the pointer is null, is it a safe operation from the point of view of the C++11/C++14 standard? struct base { virtual ~base() = default; }; struct derived : base…
ABu
  • 10,423
  • 6
  • 52
  • 103
1
vote
2 answers

How to Down Cast an Object in Java

I am working with an API in my company where I would like to create a subclass for an existing object. Here are the caveats: I cannot modify the superclass I cannot modify how the superclass object is instantiated The example I see most commonly…
Jake Fairbairn
  • 183
  • 3
  • 14
1
vote
2 answers

Type casting/Down casting in Java

I'm trying to clarify this so I fully understand type casting. Please correct me on anything that is incorrect as I've been self learning java for about 2 months now at a really slow pace. Let's say I created a class called SubObject. And I am aware…
simmonson
  • 137
  • 1
  • 9
1
vote
2 answers

Java - Upcasting and Downcasting

I Knew there are plenty of articles/questions in stackoverflow describing about upcasting and downcasting in Java. And I knew what is upcasting and downcasting. But my question is not specfic to that. Upcasting - Conversion from child to parent -…
Aishu
  • 1,310
  • 6
  • 28
  • 52
1
vote
1 answer

Trying to downcast pointer of object in std::vector

In the constructor of one of my classes I have this line : m_Projects = std::vector(); //m_Projects type is std::vector m_Current = nullptr; //m_Current type is parent_project* In a function of this same class I have…
DreamTool
  • 149
  • 1
  • 15
1
vote
1 answer

Why do we need Upcasting & Downcasting in C#?

Consider we are having three classes, Shape which is the base class and two other classes Circle and Text that inherits from the base classe (Shape) Shape.cs namespace ObjectOriented { public class Shape { public int Height { get;…
D.AhmedRafik
  • 81
  • 2
  • 15
1
vote
1 answer

Static Cast from ( Base Reference to Base Object ) to ( Derived Class Reference)

In the main function below , the first static_cast is valid as i am trying to cast a (base class reference to Derived1 class) to a derived reference But why is the second cast printing with the values, though it prints junk value for d2.y, i assumed…
LearningCpp
  • 972
  • 12
  • 29
1
vote
1 answer

Is it possible to convert instance of a class to an instance of a subclass?

I've recently came across a case where it would be very convenient to convert an instance of a class to a subclass, while the instance has been created within the parent class. But I've never seen such thing. So is there a way to do something…
Guig
  • 9,891
  • 7
  • 64
  • 126
1
vote
1 answer

Java Cloning and downcasting

Below is my sample code: class One implements Cloneable { @Override public One clone() throws CloneNotSupportedException { One obj = (One) super.clone(); return obj; } } public class Two extends One { @Override …
1
vote
2 answers

C++ : Extend object by downcasting to derived class

I guess this a classic, but in my case I have constraints that should simplify the problem. It's just that I'm getting some struct A at the input of my module, and want to add a few attributes to it. So I create a struct B which extends A, and add…
Charles
  • 988
  • 1
  • 11
  • 28
1
vote
2 answers

How can i call the child method after downcasting parent object to child object?

I have Request class which is a parent class and AddressRequest class which extends Request. public AddressRequest extends Request { private String userId; public String getUserId() { return userId; } public void…
1
vote
1 answer

incorrect type not detected by typescript compiler?

Here is a simple example. I'm using this kind of design for a MVC architecture: you can think of M (and derived classes) as a model and V (and derived classes) as a view: abstract class M { abstract update() : void; } abstract class V { abstract…
D.Fober
  • 11
  • 2