downcast

implicit upcasting and explicit downcasting in java

和自甴很熟 提交于 2019-12-04 22:01:13
问题 When java can implicitly do up casting , why does not it implicitly do down casting ?Please explain with some simple example? 回答1: The point is that upcasting will always succeed, so it's safe - whereas downcasting can fail: String x = getStringFromSomewhere(); Object y = x; // This will *always* work But: Object x = getObjectFromSomewhere(); String y = (String) x; // This might fail with an exception Because it's a "dangerous" operation, the language forces you to do it explicitly - you're

Java - Upcasting and Downcasting

♀尐吖头ヾ 提交于 2019-12-04 18:12:06
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 void getDogName(){ System.out.println("Dog"); } } public static void main(String[] args) { Dog d = new Dog

c++ casting to byte (unit8_t) during subtraction won't force underflow like I expect; output is int16_t; why?

笑着哭i 提交于 2019-12-04 06:19:28
问题 Note that byte is an 8-bit type (uint8_t) and unsigned int is a 16-bit type (uint16_t). The following doesn't produce the results that I expect. I expect it to underflow and the result to always be a uint8_t, but it becomes a signed int (int16_t) instead!!! Why? Focus in on the following line of code in particular: (byte)seconds - tStart I expect its output to ALWAYS be an unsigned 8-bit value (uint8_t), but it is instead outputting a signed 16-bit value: int16_t. How do I get the result of

Cast base instance to derived class (downcast) in C#

大城市里の小女人 提交于 2019-12-04 03:13:45
问题 Suppose I have two classes: class Employee and class AdvancedEmployee:Employee I know something like this won't work, as I can't downcast on C#: var employee = new Employee(); var advanced = employee as AdvancedEmployee; My question is: How to accomplish downcast in a efficient way? Actually I have a constructor on AdvancedEmployee that takes a Employee as parameter and use it to inject its values, basically making a clone. Update To solve the data that would get duplicated I changed the

Downcasting a list of objects in C#

做~自己de王妃 提交于 2019-12-04 02:48:25
How can I downcast a list of objects so that each of the objects in the list is downcast to an object of a derived class? This is the scenario. I have a base class with a List of base items, and two classes inheriting from it: public class BaseClass { public List<BaseItem> items; protected BaseClass() { // some code to build list of items } } public class DerivedClass : BaseClass { public DerivedClass : base() {} } public class AnotherDerivedClass : BaseClass { public AnotherDerivedClass : base() {} } public class BaseItem {} public class DerivedItem : BaseItem {} public class

wrong generic type in swift

孤者浪人 提交于 2019-12-03 20:30:31
After run following code in playgroud, why x value is 2? Is there anything wrong with swift generic type and "is" operator? class Item {} class Campaign: Item {} class AdGroup : Item {} class A<T: Item> { func val() -> Int{ let item = T() if item is Campaign { return 1 } else { return 2 } } } var m = A<Campaign>() let x = m.val() 来源: https://stackoverflow.com/questions/27899744/wrong-generic-type-in-swift

Design pattern to avoid downcasting in message passing

你。 提交于 2019-12-03 15:19:34
问题 Base class MessageHandler has derived classes. They would like to pass messages to each other. Messages could be of different classes, but can be made to share a base class. How can each MessageHandler avoid downcasting a received message? Is it somehow possible to do something that has the effect of template-parametrizing the virtual receiveMessage function on MessageHandler? Essentially, I'm trying to replace the following code with something that does not downcast, and is hopefully a

Downcasting shared pointer to derived class with additional functionality - is this safe?

核能气质少年 提交于 2019-12-03 11:48:45
问题 Consider the following outline: class Base { /* ... */ }; class Derived : public Base { public: void AdditionalFunctionality(int i){ /* ... */ } }; typedef std::shared_ptr<Base> pBase; typedef std::shared_ptr<Derived> pDerived; int main(void) { std::vector<pBase> v; v.push_back(pBase(new Derived())); pDerived p1( std::dynamic_pointer_cast<Derived>(v[0]) ); /* Copy */ pDerived p2 = std::dynamic_pointer_cast<Derived>(v[0]); /* Assignment */ p1->AdditionalFunctionality(1); p2-

Downcasting shared pointer to derived class with additional functionality - is this safe?

南笙酒味 提交于 2019-12-03 02:12:55
Consider the following outline: class Base { /* ... */ }; class Derived : public Base { public: void AdditionalFunctionality(int i){ /* ... */ } }; typedef std::shared_ptr<Base> pBase; typedef std::shared_ptr<Derived> pDerived; int main(void) { std::vector<pBase> v; v.push_back(pBase(new Derived())); pDerived p1( std::dynamic_pointer_cast<Derived>(v[0]) ); /* Copy */ pDerived p2 = std::dynamic_pointer_cast<Derived>(v[0]); /* Assignment */ p1->AdditionalFunctionality(1); p2->AdditionalFunctionality(2); /* A */ return 0; } Here I'm extending the base class with a derived class that adds

Downcasting from base pointer to templated derived types

╄→гoц情女王★ 提交于 2019-12-02 18:37:22
I have the following hierarchy: class base { public: virtual ~base(){} virtual void foo() {} }; template <typename T> class derived1 : public base { virtual void foo() {}; }; template <typename T> class derived2 : public base { virtual void foo() {}; }; Now given a pointer to base, I'd like to find out if the underlying is either derived1 or derived2. The problem is that both derived1 and derived2 can be specialised on many different types, using dynamic_cast to test for a down cast requires the template type to be know. I've ended up with messy, unmaintable and incomplete bit of code: base* b