downcast

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

笑着哭i 提交于 2019-11-27 17:57:46
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 base A In order to understand the cast system you need to dive in the object model. The classic representation of a simple hierarchy model is containment: that if B derives from A then the B object will in fact contain a A subobject alongside its own attributes. With this model, downcasting is a simple pointer manipulation, by an offset known at

Downcasting in Swift with as and as?

て烟熏妆下的殇ゞ 提交于 2019-11-27 17:35:39
问题 What's the difference between these two code snippets: let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as UITableViewCell? // vs let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as? UITableViewCell Isn't the result exactly the same? 回答1: In that code there's no difference, in both cases it evaluates to UITableViewCell? The real difference is: in the first case a downcast to UITableViewCell? is expected to always succeed (even if it's nil), so if

Downcast traits inside Rc for AST manipulation

邮差的信 提交于 2019-11-27 06:38:11
问题 I'm trying to manipulate ASTs in Rust. There will be lots of manipulations, and I want my trees to be immutable, so to save time all references will be Rc s. My tree nodes will look like this: enum Condition { Equals(Rc<Expression>, Rc<Expression>), LessThan(Rc<Expression>, Rc<Expression>), ... } enum Expression { Plus(Rc<Expression>, Rc<Expression>), ... } I want to replace a random node of a given type with another node of the same type. To do generic operations on trees I've made a trait:

Downcasting and Box<Any>

馋奶兔 提交于 2019-11-27 05:36:23
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 + 'static everywhere. pub struct WidgetWrap { // ... widget: RefCell<Box<Any + 'static>>, } let mut cell

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

笑着哭i 提交于 2019-11-27 04:14:19
问题 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 base A 回答1: In order to understand the cast system, you need to dive into the object model. The classic representation of a simple hierarchy model is containment: if B derives from A then the B object will, in fact, contain an A subobject alongside

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

谁说胖子不能爱 提交于 2019-11-27 02:45:25
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 = dict.objectForKey("SomeKey") as! String? The practical difference is this: var optionalString = dict["SomeKey"] as? String optionalString will be a variable of type String? . If the underlying type is something other than a String this will harmlessly just assign nil to the optional. var optionalString = dict["SomeKey"] as! String? This says, I know this thing is a

C++ inheritance downcasting

橙三吉。 提交于 2019-11-27 02:39:50
问题 I have my base class as follows: class point //concrete class { ... //implementation } class subpoint : public point //concrete class { ... //implementation } How do I cast from a point object to a subpoint object? I have tried all three of the following: point a; subpoint* b = dynamic_cast<subpoint*>(&a); subpoint* b = (subpoint*)a; subpoint b = (subpoint)a; What is wrong with these casts? 回答1: How do I cast from a point object to a subpoint object? You can't; unless either point has a

downcast and upcast

╄→гoц情女王★ 提交于 2019-11-26 21:49:31
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 = (Employee)mgr; Here Employee is a Manager , but when I cast it like that to an Employee it means I am upcasting it? Question 2 : When I have several Employee class objects and some but not all of them are Manager 's, how can I downcast them where possible? RCIX That is correct. When you do that you are casting it it into an employee object, so that means you

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

若如初见. 提交于 2019-11-26 17:29:47
问题 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 = dict.objectForKey("SomeKey") as! String? 回答1: The practical difference is this: var optionalString = dict["SomeKey"] as? String optionalString will be a variable of type String? . If the underlying type is something other than a String this will harmlessly just

explicit type casting example in java

ε祈祈猫儿з 提交于 2019-11-26 15:09:44
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"; } } public class InstanceOfExample { static boolean result; static HeavyVehicle hV = new HeavyVehicle();