Questions tagged [upcasting]

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

244 questions
160
votes
9 answers

What is the difference between up-casting and down-casting with respect to class variable

What is the difference between up-casting and down-casting with respect to class variable? For example in the following program class Animal contains only one method but Dog class contains two methods, then how we cast the Dog variable…
Dhivakar
  • 2,081
  • 5
  • 15
  • 18
114
votes
12 answers

Why do we assign a parent reference to the child object in Java?

I am asking a quite simple question, but I am bit confused in this. Suppose I have a class Parent: public class Parent { int name; } And have another class Child: public class Child extends Parent{ int salary; } And finally my Main.java…
Narendra Pal
  • 6,474
  • 13
  • 49
  • 85
104
votes
6 answers

downcast and upcast

I am new to C# (and OOP). When I have some code like the following: class Employee { // some code } class Manager : Employee { //some code } Question 1: If I have other code that does this: Manager mgr = new Manager(); Employee emp…
user184805
  • 1,839
  • 4
  • 19
  • 16
24
votes
9 answers

How do upcasting and vtables work together to ensure correct dynamic binding?

So, vtable is a table maintained by the compiler which contains function pointers that point to the virtual functions in that class. and Assigning a derived class's object to an ancestor class's object is called up-casting. Up-casting is handling a…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
24
votes
3 answers

Can I cast a derived class to a private base class, using C-style cast?

Can I do this? class A { ... }; class B : private A { const A &foo() const { return *((const A *)this); } }; Can I take a subclass that inherits privately from a base class and cast it to a public version of its base class? Can…
AdamIerymenko
  • 1,299
  • 2
  • 10
  • 19
15
votes
2 answers

Does upcasting a null pointer lead to undefined behavior

I'm wondering whether the following code leads to undefined behavior: #include #include struct IA { virtual ~IA() {} int a = 0; }; struct IB { virtual ~IB() {} int b = 0; }; struct C: IA, IB {}; int main() { C* pc =…
Lingxi
  • 14,579
  • 2
  • 37
  • 93
12
votes
1 answer

Why is upcasting necessary in this Scala code?

This compiles: import scala.collection._ trait Foo[A, +This <: SortedSet[A] with SortedSetLike[A,This]] extends SortedSetLike[A, This] { this: This => def bar: This = (this: SortedSetLike[A,This]).empty } But if the upcast is removed it fails…
Robin Green
  • 32,079
  • 16
  • 104
  • 187
11
votes
2 answers

Implicit conversion vs. static_cast when upcasting

Let's say I have three classes: A (the mother, abstract), and B and C, the children of A. So B and C inherit from A (public inheritance). I have a list of pointers to A, which I populate with pointers of B or C. The question is: which is the…
fern17
  • 467
  • 6
  • 20
11
votes
3 answers

Why Java object class remains same after casting?

I tried to upcast an objet. But at runtime object class is remained as a derived class. Derived drv = new Derived(); Base base = (Base) drv; System.out.println("Class : " + base.getClass()); //prints -> Class : class packagename.Derived So…
Huseyin
  • 163
  • 8
11
votes
5 answers

How Derived Class object is added to Base Class objects List

Given the following code, I have inherited a class Circle from Shape: class Shape { void Draw(); } class Circle : Shape { } void Main(string[] args) { Shape s = new Shape(); Shape s2 = new Shape(); Circle c = new Circle(); …
Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
10
votes
2 answers

up-casting std::shared_ptr using std::dynamic_pointer_cast

I'm starting to work with smart pointers in C++0X/11 and I've run into a peculiar situation. I want to up cast an instance of an object using shared_ptr. Class Extend inherits from class Base, where Base class has a virtual destructor in order to…
Ælex
  • 14,432
  • 20
  • 88
  • 129
10
votes
3 answers

Why does C style cast allow you to convert to a private base class?

Say we have this code class A { public: A() : x(1) {} virtual ~A() {} int x; }; class B { public: B() : y(2) {} virtual ~B() {} void g() { cout << "B::" << y << endl; } int y; }; class C : private A,…
unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
9
votes
5 answers

How to implement a compile-time check that a downcast is valid in a CRTP?

I have a plain old CRPT (please don't get distracted by access restrictions - the question is not about them): template class Base { void MethodToOverride() { // generic stuff here } void…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
9
votes
4 answers

Upcasting and its effect on the heap

For the following classes: public class Parent { //Parent members } public class ChildA : Parent { //ChildA members } public class ChildB : Parent { //ChildB members } If I upcast ChildA or ChildB instance to a Parent instance, then I can't…
Honey
  • 169
  • 1
  • 8
9
votes
1 answer

implicit upcasting and explicit downcasting in java

When java can implicitly do up casting , why does not it implicitly do down casting ?Please explain with some simple example? …
Number945
  • 4,631
  • 8
  • 45
  • 83
1
2 3
16 17