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

Casting of classes ( upcasting )

I have a class named "obj" and another one named "aggregate" . The second one is derived from the first one. The class "obj" holds only an integer. The "aggregate" nothing more ( only more "methods"). class obj{ public: int t; } I have a function…
George Kourtis
  • 2,381
  • 3
  • 18
  • 28
0
votes
1 answer

Downcasting: tree construction

I wrote a spike solution for a basic tree building class that I created. The Output for the first "Adding Item No {0} at the depth of {0}" is item 0 depth 0, not the expected 0,1. It just hit me as I was writing this out. Could it be due to my down…
0
votes
1 answer

Upcasting the downcasted object back in protobuffers

Below is the proto code, message Animal { optional string name = 1; optional int32 age = 2; } message Dog{ optional string breed = 1; } Then the sample class using the above proto public class InheritanceTest { public…
Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
0
votes
2 answers

Upcasting a template object on function parameter failed

In the following simplified code I try something like this: struct A{}; struct B : public A {}; void func(A &a) {} B b; func(b); Typically this is working but in the following more complicated code it doesn't work. I guess that I missing something…
Chameleon
  • 1,804
  • 2
  • 15
  • 21
0
votes
1 answer

up-casting to template class using custom cast operator

I have a class inheriting from two classes, one which is my own base class, and a template class: typedef typename cusp::csr_matrix< int, float, cusp::host_memory >…
Ælex
  • 14,432
  • 20
  • 88
  • 129
0
votes
1 answer

Does compiler optimizes unnecessary/redundant upcast away or Does it produce any IL at all?

From Do redundant casts get optimized? I can see compiler doesn't optimizes unnecessary downcast (i.e. castclass) away. But now I am interested in a simpler case, " if compiler optimizes unnecessary upcast away?" This question only concerns…
colinfang
  • 20,909
  • 19
  • 90
  • 173
0
votes
2 answers

Converting a returned object of TypeA to a type derived from TypeA

I'm calling a method which I do not own that returns a 'Task' object. I want to return that object from my method, but I also need to override the Exception property of that object. If the underlying task catches an exception, and my user retrieves…
0
votes
2 answers

how to get the implementation of a superclass from a subclass using upcasting or by other methods?

i just wanted to know how to get the implementation of a superclass using a subclass, for example. class Animal { void poo() { System.out.println("general poo"); } } class Horse extends Animal{ void poo() { …
chip
  • 3,039
  • 5
  • 35
  • 59
0
votes
3 answers

Do Upcasting effects on Static methods?

Why It calls base class method when we declare method as static in base as well as in derive class and do upcasting. class Base { static void show(){ System.out.println("Base class...."); } } class Derive extends Base { static…
Akhilesh Dhar Dubey
  • 2,152
  • 2
  • 25
  • 39
0
votes
3 answers

Reversal of Upcasting for Component system

I am currently working on a Component based game engine written in c++. All components inherit from a component base class. All components in the scene are upcasted into a vector of Components were they can be iterated over and Update() and such can…
DavidColson
  • 8,387
  • 9
  • 37
  • 50
0
votes
1 answer

Upcasting to superclasses or interfaces?

I am trying confirm the theory behind something that I have already got working in practice. The full setup is somewhat contorted, as the functionality is split between different dlls, but I'll try to describe the situation: class __declspec(…
Mike Sadler
  • 1,750
  • 1
  • 20
  • 37
-1
votes
1 answer

Is upcasting from the current object reference or actual object type?

I encountered a question in school which asked for the the kind of typecasting and whether an error would occur. InterfaceA obj = new ClassB(); ClassA obj2 = obj; I understand that the first line would cause a compile time error due to ClassB being…
bbawj
  • 46
  • 1
  • 5
-1
votes
1 answer

While Upcasting I called method of base class(with virtual keyword) but after upcasting its calling override method from derived class

I am trying to understand Upcasting and DownCasting. In the code shown below, I upcast Circle object to Shape and after upcasting methods available are Draw and Duplicate, but when I executed shape.Draw() it is showing output from derived class -…
baudi27
  • 25
  • 5
-1
votes
2 answers

What should be the result of overridden "Object#equals(Object)" when instance is upcasted?

I am, specifically concerned with obeying the symmetry part of the general contract established in Object#equals(Object) where, given two non-null objects x and y, the result of x.equals(y) and y.equals(x) should be the same. Suppose you have two…
-1
votes
3 answers

How to access child class method using parent class refrence?

I am getting error while accessing Child class method by using Parent class reference variable. Please help me. How can I access that method? class Parent { public void show() { System.out.println("Show method in Parent class"); …