Questions tagged [downcast]

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

545 questions
190
votes
3 answers

In Objective-C, what is the equivalent of Java's "instanceof" keyword?

I would like to check whether an object (e.g. someObject) is assignable (cast-able) to a variable of another type (e.g. SpecifiedType). In Java, I can write: someObject instanceof SpecifiedType A related question is finding whether the runtime type…
Dimitris
  • 672
  • 3
  • 14
  • 18
149
votes
10 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
  • 1,901
  • 5
  • 12
  • 17
133
votes
6 answers

Convert base class to derived class

Is it possible in C# to explicitly convert a base class object to one of it's derived classes? Currently thinking I have to create a constructor for my derived classes that accept a base class object as a parameter and copy over the property values.…
ARW
  • 2,806
  • 6
  • 30
  • 40
97
votes
9 answers

Downcasting optionals in Swift: as? Type, or as! Type?

Given the following in Swift: var optionalString: String? let dict = NSDictionary() What is the practical difference between the following two statements: optionalString = dict.objectForKey("SomeKey") as? String vs optionalString =…
sdduursma
  • 2,521
  • 2
  • 14
  • 15
94
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,711
  • 3
  • 18
  • 16
92
votes
28 answers

Is it possible to assign a base class object to a derived class reference with an explicit typecast?

Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?. I have tried it and it creates a run-time error.
Maddy.Shik
  • 6,241
  • 15
  • 62
  • 98
75
votes
3 answers

How does one downcast a std::shared_ptr?

Consider: struct SomethingThatsABase { virtual bool IsChildOne() const { return false; } virtual bool IsChildTwo() const { return false; } }; struct ChildOne : public SomethingThatsABase { virtual bool IsChildOne() const { return true;…
Billy ONeal
  • 97,781
  • 45
  • 291
  • 525
60
votes
7 answers

C++ cannot convert from base A to derived type B via virtual base A

I have three classes: class A {}; class B : virtual public A {}; class C : virtual public A {}; class D: public B, public C {}; Attempting a static cast from A* to B* I get the below error: cannot convert from base A to derived type B via virtual…
57
votes
3 answers

When is upcasting illegal in C++?

I am pretty sure I understand the general difference between upcasting and downcasting, particularly in C++. I understand that we can't always downcast because casting a base class pointer to a derived class pointer would assume that the base class…
Connor Foley
  • 568
  • 4
  • 9
44
votes
6 answers

Why can't static_cast be used to down-cast when virtual inheritance is involved?

Consider the following code: struct Base {}; struct Derived : public virtual Base {}; void f() { Base* b = new Derived; Derived* d = static_cast(b); } This is prohibited by the standard ([n3290: 5.2.9/2]) so the code does not…
eran
  • 20,710
  • 4
  • 52
  • 86
37
votes
5 answers

Cast the current object ($this) to a descendent class

I have a class where it may be necessary to change the object to a descendent class further down the line. Is this possible? I know that one option is to return a copy of it but using the child class instead, but it'd be nice to actually modify the…
Nathan MacInnes
  • 10,685
  • 4
  • 33
  • 47
37
votes
3 answers

Treating a forced downcast as optional will never produce 'nil'

I've been playing around with Swift and discovered that when down casting an object to be inserted into a dictionary, I get a weird warning: Treating a forced downcast to 'String' as optional will never produce 'nil'. If I replace as with as? then…
Pamelloes
  • 660
  • 1
  • 6
  • 12
34
votes
6 answers

Explicit type casting example in Java

I have come across this example on http://www.javabeginner.com/learn-java/java-object-typecasting and in the part where it talks about explicit type casting there is one example which confuses me. The example: class Vehicle { String name; …
SineLaboreNihil
  • 933
  • 4
  • 11
  • 18
20
votes
4 answers

How to downcast a Java object?

I am trying to understand Java's polymorphism, and I have one question about downcasting an object. Let's say for this example I have two subclasses Dog and Cat that inherit from a superclass Animal From what I understood, the only way to downcast…
nbarraille
  • 9,158
  • 14
  • 60
  • 92
19
votes
2 answers

Dynamic downcast on private inheritance within private scope

A tweak on this question that I've run into. Consider: class A {}; class B : private A { static void foo(); }; void B::foo(){ B* bPtr1 = new B; A* aPtr1 = dynamic_cast(bPtr1); // gives pointer B* bPtr2 = dynamic_cast(aPtr1); //…
Ziv
  • 2,325
  • 3
  • 21
  • 37
1
2 3
36 37