downcast

Defining a method for a struct only when a field is a certain enum variant?

别等时光非礼了梦想. 提交于 2021-02-02 09:26:52
问题 I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, length: usize, }, Directory { parent_index: usize, next_index: usize, }, } Entry is an entry in a GameCube ROM file system table which describes a file or directory. I defined various methods for Entry such as Entry::read_filename and Entry::write_to_disk . However, I have some

Defining a method for a struct only when a field is a certain enum variant?

放肆的年华 提交于 2021-02-02 09:21:45
问题 I have the following struct: #[derive(Debug)] pub struct Entry { pub index: usize, pub name: String, pub filename_offset: u64, pub entry_type: EntryType, } #[derive(Debug)] pub enum EntryType { File { file_offset: u64, length: usize, }, Directory { parent_index: usize, next_index: usize, }, } Entry is an entry in a GameCube ROM file system table which describes a file or directory. I defined various methods for Entry such as Entry::read_filename and Entry::write_to_disk . However, I have some

How to cast nil interface to nil other interface

蓝咒 提交于 2020-05-30 09:36:52
问题 I have a classic Go nil interface issue. I'm trying to assert an interface{} , which I assign from a nil error , back to an error interface. That sentence is confusing so I have a handy-dandy example: https://play.golang.com/p/Qhv7197oIE_z package main import ( "fmt" ) func preferredWay(i interface{}) error { return i.(error) } func workAround(i interface{}) error { if i == nil { return nil } return i.(error) } func main() { var nilErr error fmt.Println(workAround(nilErr)) // Prints "<nil>"

Upcasting/Downcasting in Java

痞子三分冷 提交于 2020-02-14 02:32:30
问题 I am trying to understand upcasting and downcasting in Java and I am confused by the following scenario (about my code, which is below): First - why is it that the code does not compile when I include the line myAnimal.bark(); , and Second - (assuming I comment out myAnimal.bark(); ) why does calling myAnimal.move() print "moveDog" instead of "moveAnimal" ? Isn't myAnimal restricted to methods from the Animal class because we have declared its type to be Animal , even though we are setting it

How to change this design to avoid a downcast?

我们两清 提交于 2020-01-23 02:05:08
问题 Let's say I have a collection of objects that all inherit from a base class. Something like... abstract public class Animal { } public class Dog :Animal { } class Monkey : Animal { } Now, we need to feed these animals, but they are not allowed to know how to feed themselves. If they could, the answer would be straightforward: foreach( Animal a in myAnimals ) { a.feed(); } However, they can't know how to feed themselves, so we want to do something like this: class Program { static void Main

Why upcasted ab.a + ab.b produce result 1 and 1.0? [duplicate]

懵懂的女人 提交于 2020-01-11 14:20:34
问题 This question already has answers here : Is there a way to override class variables in Java? (17 answers) Why is super class constructor always called [duplicate] (3 answers) Closed 6 days ago . I don't get how we managed to invoke constructor without parameters in class A at all. How upcasting works in this particular example? When we produce A ab = bb; what exactly ab refers to? public class A { public Integer a; public Float b; public A() { a = 1; b = 1.0f; } public A(Integer x) { a = 2; b

Should/how can I avoid downcasting in this case?

安稳与你 提交于 2020-01-05 05:10:49
问题 Say I have a base and derived class, where the derived class implements some additional manufacture specific functionality: class Device { // Base class } class DeviceFromSpecificManufacture : public Device { // Derived } When my program runs, it requires the user to select a device from an array of available devices. At this point, it's fine for me to use the base class as I only require basic device functionality (nothing specific to the manufacture): std::vector<std::shared_ptr<Device>>

Java - Upcasting and Downcasting

帅比萌擦擦* 提交于 2020-01-01 19:40:16
问题 I Knew there are plenty of articles/questions in stackoverflow describing about upcasting and downcasting in Java. And I knew what is upcasting and downcasting. But my question is not specfic to that. Upcasting - Conversion from child to parent - Compiler takes care. No cast is required Downcasting - Conversion from parent to child - Explicit cast is required public class Animal { public void getAnimalName(){ System.out.println("Parent Animal"); } } public class Dog extends Animal{ public

C++ dynamic_cast - polymorphic requirement and downcasting

偶尔善良 提交于 2019-12-30 03:11:10
问题 In the following code, while constructing obj in case 1, we construct a derived class object too, but its member functions are just inaccessible to obj . So while downcasting (i.e., in case 2), using obj as source, we have the constructed derived in it already. Why would obj need to be polymorphic? If I confused you with my above description, why doesn't obj need to be polymorphic when upcasting, but while downcasting it does need to be polymorphic while using dynamic_cast ? class base {

Java downcasting and is-A has-A relationship

一笑奈何 提交于 2019-12-25 16:52:17
问题 HI, I have a down casting question, I am a bit rusty in this area. I have 2 clasess like this: class A{ int i; String j ; //Getters and setters} class B extends A{ String k; //getter and setter} I have a method like this, in a Utility helper class: public static A converts(C c){} Where C are objects that are retireved from the database and then converted. The problem is I want to call the above method by passing in a 'C' and getting back B. So I tried this: B bClasss = (B) Utility.converts(c)