Questions tagged [upcasting]

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

244 questions
3
votes
3 answers

If two values have different data types, Java will automatically promote one of the values to the larger of the two data types?

The rule says that if two values have different data types, Java will automatically promote one of the values to the larger of the two data types? In the code below, the value assigned to y is larger than the value of x. Therefore, taking the rule…
kaka
  • 597
  • 3
  • 5
  • 16
3
votes
2 answers

Is there a point in upcasting "this" reference in Java?

I have come across a weird piece of code. I was wondering if there is any usage for it. class C extends B { int xB = 4; C() { System.out.println(this.xB); System.out.println(super.xB); …
Michael
  • 577
  • 1
  • 11
  • 21
3
votes
1 answer

Can't upcast pointer to pointer argument

I can't understand anything here. I expected that if I can pass a dog pointer to function taking animal pointer, I could also pass the &dog to a function that takes a pointer to pointer of Animal. struct Animal{}; struct Dog : Animal{}; void…
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
3
votes
2 answers

Swift - upcasting array of protocol to array of super protocol causes error

In Swift, I notice that I can upcast an object that conforms to a protocol called, let's say SubProtocol to another protocol called SuperProtocol which is a super protocol of SubProtocol. But I can't do the same with an array of the protocol. Here's…
Jake
  • 1,518
  • 2
  • 14
  • 20
3
votes
1 answer

Does SFINAE apply to function bodies?

I have the following sample code: class Serializable {}; class MyData : public Serializable {}; void GetData( Serializable& ) {} template void GetData( T& data ) { std::istringstream s{"test"}; s >> data; } int main() { …
void.pointer
  • 24,859
  • 31
  • 132
  • 243
3
votes
5 answers

Upcasting pointer reference

I have the following contrived example (coming from real code): template class Base { public: Base(int a):x(a) {} Base(Base * &other) { } virtual ~Base() {} private: int x; }; template class Derived:public…
user231536
  • 2,661
  • 4
  • 33
  • 45
3
votes
4 answers

Upcast/Downcast and serialization

Just playing around with casting. Assume, we have 2 classes public class Base { public int a; } public class Inh : Base { public int b; } Instantiate both of them Base b1 = new Base {a = 1}; Inh i1 = new Inh {a = 2, b =…
Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64
3
votes
1 answer

Casting of derived class to one of the bases through base pointer

EDIT: Ok as I see now this changes the case a lot so the more precise scenario is as such: The hierarchy I currently have is somewhat similar to this: class IBase() { virtual void Foo() = 0; }; class Base() : public IBase { virtual void Foo() { }…
Adrian Lis
  • 647
  • 4
  • 20
3
votes
5 answers

inheritance and container in C#

I work in C# here, let's say i have: class A {} class B : A {} List myList; I would like, in a part of the code cast this myList as List< A>, but when I try to, I get an error: List myUpcastedList = (List)myList; //not working is it…
Titan
  • 181
  • 2
  • 9
3
votes
1 answer

F# upcasting base

I get a parse error when I want to upcast base to the appropriate interface type (i.e. A) such that I can call doA() on it. I'm aware that base (http://cs.hubfs.net/topic/None/58670) is somewhat special, but I've not been able to find a work around…
Ruben
  • 33
  • 1
  • 4
3
votes
2 answers

Is it safe to upcast a function pointer?

I have base classes Object and an Event class Object { //... }; class Event { }; And a typedef for a function pointer typedef void (Object::*PF) (Event*); And an unrelated class which stores two pointers class Dispatcher { public: Dispatcher…
user1773602
3
votes
2 answers

In C# Do I Ever Have to Perform An Explicit Upcast?

Given a class hierarchy with a base class B and a subclass S: class B { } class S : B { } I can assign a S to an B with an implicit conversion: B b = new S(); If I wanted to downcast this back to a S I would have to do this explicitly: B b = new…
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
3
votes
4 answers

Can an upcasted object be downcasted again without trying a cast for every derived class type of the base class type?

I have case where am given a collection of objects that all derive from the same base class. If I iterate over the collection and check each item's type, I can see that the object is of a derived type and then handle it accordingly. What I would…
JWilliams
  • 115
  • 1
  • 9
3
votes
1 answer

Java wildcards don't allow type-casting

interface Message { } interface Foo { void frob(Message> message); } class AuxiliaryFoo implements Foo { @Override public void frob(Message> message) { } } class MainFoo implements…
Joker_vD
  • 3,715
  • 1
  • 28
  • 42
3
votes
2 answers

why is a derived class's virtual method called after upcasting?

After upcasting a derived class's pointer, a virtual method of the derived class is still called, which seems to me wrong, as slicing should have happened. Could you please comment what's wrong with this code? class Base { public: virtual void…
rightaway717
  • 2,631
  • 3
  • 29
  • 43