downcast

Downcasting/Upcasting error at compile time & runtime?

旧街凉风 提交于 2019-12-19 04:02:00
问题 Please check the below program. I have doubt when compiler will issue casting exception at compiler level and when it will be at runtime ? Like in below program, expression I assumed (Redwood) new Tree() should have failed at compiler time as Tree is not Redwood. But it is not failing in compile time , as expected it failed during runtime !!! public class Redwood extends Tree { public static void main(String[] args) { new Redwood().go(); } void go() { go2(new Tree(), new Redwood()); go2(

How to downcast a Java object?

南笙酒味 提交于 2019-12-18 10:58:18
问题 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 an object is if this Object is already of the good type, like this: Animal a = new Dog(); Dog d = (Dog) a; This works right? But what if I want to create a regular animal without knowing what it would be, and then cast it when I know, how can I do

Downcasting with Entity Framework

大憨熊 提交于 2019-12-17 20:53:41
问题 I have a project where I've defined in EF an Employer as a derived class of User . In my process I create a user without knowing whether it will eventually be an employer (or other kinds of users) and later I need to convert it. At first I tried (Intellisense indicated an explicit conversion exists): Employer e = (Employer) GetUser(); but at runtime I got: Unable to cast object of type 'System.Data.Entity.DynamicProxies.User_7B...0D' to type 'Employer'. so I tried to write a converter: public

Downcasting and Box<Any>

你说的曾经没有我的故事 提交于 2019-12-17 07:55:47
问题 pub struct WidgetWrap { // ... widget: RefCell<Box<Any>>, } At some point I want to cast Box<Any> to Box<WidgetTrait> let mut cell = widget.borrow_mut(); let w = cell.downcast_mut::<Box<WidgetTrait>>(); This gives me an error of this kind: error: instantiating a type parameter with an incompatible type `Box<WidgetTrait>`, which does not fulfill `'static` [E0144] What does this really mean? I've looked at How to fix: value may contain references; add `'static` bound to `T` and did try adding +

explicit type casting example in java

假如想象 提交于 2019-12-17 03:00:19
问题 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; Vehicle() { name = "Vehicle"; } } class HeavyVehicle extends Vehicle { HeavyVehicle() { name = "HeavyVehicle"; } } class Truck extends HeavyVehicle { Truck() { name = "Truck"; } } class LightVehicle extends Vehicle { LightVehicle() { name = "LightVehicle"; }

Validity of casting a Base pointer to a Derived pointer when Derived only adds methods

不打扰是莪最后的温柔 提交于 2019-12-14 02:17:43
问题 First, the question is very similar to downcasting shared pointer to derived class with additional functionality is, where there are good answers. But I'd like to have explanation on why this is valid (or not) and when not using shared pointers. So : class Base { /* ... */ }; class Derived : public Base { public: void additionnalFunc(int i){ /* access Base members */ } }; int main(){ Base b; Derived* d = (Derived*) &b; // or static_cast ? d->additionnalFunc(3); // works ok. } This works as

How come when downcasting I don't have to use an initializer of ItemsTableViewController? Is it initialized?

99封情书 提交于 2019-12-13 08:08:43
问题 I'm working through a tutorial and I noticed that when downcasting I didn't have to use an initializer method of the object. Is the object initialized? In the AppDelegate codebase below, I'm referring to the ItemsTableViewController. All I had to do was say "as!" but didn't need to use an init method or double parenthesis like this "ItemsTableViewController()". Is the ItemsTableViewController initialized and if so how? // // AppDelegate.swift // HomepwnerThirdTime // // Created by Laurence

How can I take input from either stdin or a file if I cannot seek stdin?

筅森魡賤 提交于 2019-12-12 10:56:19
问题 I am porting some Python to Rust as a learning exercise and need to take input from either a file or stdin. I keep a handle to my input in a struct so I thought I'd just make a Box<io::Read> but I ran into a situation where I need to seek on the input, and seek isn't part of the Read trait. I know you can't seek in pipes, so I'm going ahead and assuming for now that this method only gets called when the input is a file, but my problem is that I can't check that and downcast in Rust. I know

How do I cast a List from a subclass generic to a parent class generic?

橙三吉。 提交于 2019-12-12 01:38:43
问题 I'm modifying open JDK to add features and I've run into this twice with no good solution. There's a class named JCStatement which extends JCTree . Issue: I want to cast a List<JCStatement> into a List<JCTree> . It's clear that a class can reference one of its extensions, but when I have it on a List, it just doesn't work. I used: (List<JCTree>)((List<?>)params) to cast, which works, but doesn't build on ant. IDE gives me the following warning: Type safety: Unchecked cast from List<capture#1

Detect Object type then cast it accordingly?

南笙酒味 提交于 2019-12-11 23:51:09
问题 My method takes as input an Object. How do i determine it's type, then cast it accordingly? So for example: binarySearch( Object o ); Inside the binarySearch method, i need a way to determine the type (or class) of Object o. Then i need to cast it with that type. how would i do that??? And more specifically, Object o is a child of a base class (EG SalariedEmp is child of Employee), and i specifically need the base class. EDIT: I figured out how to do what I wanted, which really should be a