Questions tagged [upcasting]

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

244 questions
0
votes
1 answer

C++ A polymorphic class, virtual function and casting for performance

I have the following classes: class State { protected: Vec3D accel; Vec3D gyro; Vec3D gps; float reward; public: boost::ptr_vector actions; ... virtual bool isTerm(); } class guState : public State { …
Ælex
  • 14,432
  • 20
  • 88
  • 129
0
votes
2 answers

Changing one row in 2D ArrayList in java changes all the rows

I want to book one seat in a cinema hall according to input but when I am trying to do that it changes all the rows. Booked seat is denoted by 'B'. Method that changes the state of cinema hall: public void bookSeat(int row, int seat) { …
0
votes
1 answer

upcasting to interface with private inheritance

I have the following situation class Interface { }; class ComplicatedObject : public Interface { }; and I would like to give a simplified version of the ComplicatedObject, still providing the Interface. I was thinking to private inheritance, such…
Teloze
  • 279
  • 2
  • 8
0
votes
0 answers

Explicit up casting & Implicit up casting

I'm trying to find the difference between Explicit up casting and Implicit up casting but there's not so much posts about them. Somebody help me pls?
0
votes
1 answer

How to upcast an static std::optional

class Child: public Base {.....} class Child { static std::optional& Child::GetBaseInstance() { static std::optional instance = std::make_optional; \\Does not work. Please help return instance; } } I'm…
rstr1112
  • 308
  • 4
  • 13
0
votes
0 answers

C++ AST Design, best way to modify upcasted Node members to change children

I need some advice on my AST design. I'm using a pretty typical AST node system for a simple interpreter. At this point I'm trying to implement simple constant propagation. This involves transformations of the node tree which has led to some…
Tom
  • 1,235
  • 9
  • 22
0
votes
1 answer

Is an object accessible from a previous reference after it has been upcast (in Java)?

Say A is a parent class. Let B and C be its child classes. A a; B b = new B(args); a = b; Is the type B object still accessible via b, or has it been permanently upcast to type A?
user11991978
0
votes
2 answers

When upcasting an object, what parts refer to the parent and what parts refer to the child?

In the example below, I am confused as to why upcasting seems to refer to some parts of the parent class and some parts of the actual class. public class B extends A{ int fi = 15; public static void main(String[] args){ B b = new B(); …
0
votes
1 answer

c# How to put all dictionary into an object of derived type from Dictionary?

class XDictionary : Dictionary { } Dictionary d = new Dictionary { { "x", "xx" }, { "y", "yy" }, { "z", "zz" } }; …
0
votes
1 answer

A trick to enable casting over non-public inheritances

This is a followup related to my previous question where I investigated the problem and found that up- and downcasting seem to be working correctly over public inheritance relations. For example, this code does not even compile: class A { }; class…
peterh
  • 11,875
  • 18
  • 85
  • 108
0
votes
0 answers

How is this casting happening in the following java code

I do not understand how the casting is done on var2 The following line does not Compile as methodA1() not present in interface reference I1 String var = i1.methodA1(); Hence I1 requires a cast to invoke methodA1 I do not understand how the casting…
Anshul Gupta
  • 265
  • 2
  • 12
0
votes
1 answer

Upcasting; why do i get the output 11

So i ran this code and I don't understand why I got the output 11: class Parent{ protected int counter; public Parent(){counter++;} } class Child extends Parent{ public Child(){ System.out.print(counter);} } } public class Test{ …
silvermaze
  • 133
  • 1
  • 1
  • 3
0
votes
1 answer

Java Casting To Parent Type and ignore child type attributes

I am trying to cast a child type to a parent type. The thing is since the child class extends parent class, when casting to the parent type child specific attributes are also kept. I would like to leave out some of the child class attributes and…
Nesan Mano
  • 1,892
  • 2
  • 26
  • 43
0
votes
1 answer

Why is automatic upcasting from unique_ptr to unique_ptr failing in this example?

ControllerSender is the base class, and several classes will be direct children of that (only BasicSender is in this example) and instantiated through a factory function MidiControllers::AddSender. A pointer to the instantiated object, along with…
rsjaffe
  • 5,600
  • 7
  • 27
  • 39
0
votes
3 answers

Upcasting/Downcasting in Java

I am trying to understand upcasting and downcasting in Java and I am confused by the following scenario (about my code, which is below): First - why is it that the code does not compile when I include the line myAnimal.bark();, and Second -…
coolcow
  • 45
  • 6