downcast

downcasting in php5

只愿长相守 提交于 2019-12-02 10:05:09
问题 I've realized that there's no downcasting in php5. Is there a common pattern to achieve it? 回答1: You could set the derived class to take a BaseClass object as a parameter in the constructor, and then copy the properties from that: class Base { var $x, $y; } class DerivedClass extends Base { function __construct($param) { $this->copyFromBase($param); // put some type-checking here... } function copyFromBase($base) { $this->x = $base->x; // you could definitely use a more $this->y = $base->y; /

downcasting in php5

≯℡__Kan透↙ 提交于 2019-12-02 05:25:01
I've realized that there's no downcasting in php5. Is there a common pattern to achieve it? You could set the derived class to take a BaseClass object as a parameter in the constructor, and then copy the properties from that: class Base { var $x, $y; } class DerivedClass extends Base { function __construct($param) { $this->copyFromBase($param); // put some type-checking here... } function copyFromBase($base) { $this->x = $base->x; // you could definitely use a more $this->y = $base->y; // intelligent way to do this } } $b = new Base(); $b->x = 'X'; $b->y = 'Y'; $b = new Derived($b); 来源: https:

Java inheritance downcast ClassCastException

五迷三道 提交于 2019-12-02 02:48:00
问题 given the following code, I have a question: class A{} class B extends A {} class C extends B{} public class Test { public static void main(String[] args) { A a = new A(); A a1=new A(); B b = new B(); // a=b;// ok // b=(B)a;// ClassCastException // a=(A)a1; // ok // a=a1; // ok a=(B)a1; // compiles ok, ClassCastException } } My question is with the line in bold. My understanding is that for the code to compile, it just needs to be satisfied that the classes are in the same hierarchy and as a

Java inheritance downcast ClassCastException

泄露秘密 提交于 2019-12-02 00:51:51
given the following code, I have a question: class A{} class B extends A {} class C extends B{} public class Test { public static void main(String[] args) { A a = new A(); A a1=new A(); B b = new B(); // a=b;// ok // b=(B)a;// ClassCastException // a=(A)a1; // ok // a=a1; // ok a=(B)a1; // compiles ok, ClassCastException } } My question is with the line in bold. My understanding is that for the code to compile, it just needs to be satisfied that the classes are in the same hierarchy and as a result it may work (up the tree implicit casting, down the tree requires explicit casting). Whenever I

Why Downcasting throws Exception?

a 夏天 提交于 2019-12-01 20:18:25
问题 In java: Base b = new Base(); Derived d = (Derived)b; throws ClassCastException . Why? Why downcasting throws Exception here? I could not figure out the reason. 回答1: Let me rename your classes to make things more clear. Base -> Animal . Derived -> Cat . Just because you're an Animal doesn't mean you're a Cat . You could be a Dog . That's why it's illegal to cast an Animal into a Cat . On the other hand, is every Cat an Animal ? The answer is "yes". That's why you could write code like this:

Why Downcasting throws Exception?

无人久伴 提交于 2019-12-01 19:30:44
In java: Base b = new Base(); Derived d = (Derived)b; throws ClassCastException . Why? Why downcasting throws Exception here? I could not figure out the reason. Let me rename your classes to make things more clear. Base -> Animal . Derived -> Cat . Just because you're an Animal doesn't mean you're a Cat . You could be a Dog . That's why it's illegal to cast an Animal into a Cat . On the other hand, is every Cat an Animal ? The answer is "yes". That's why you could write code like this: Animal animal = new Cat(); or Cat cat = new Cat(); Animal animal = cat; Also what's worth noting is you can

Why can't I downcast pointer to members in template arguments?

廉价感情. 提交于 2019-12-01 18:33:23
问题 If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work? struct Foo { int x; }; struct Bar : public Foo { }; template<class T, int T::* z> struct Buzz { }; static int Bar::* const workaround = &Foo::x; int main() { // This works. Downcasting of pointer to members in

Why can't I downcast pointer to members in template arguments?

こ雲淡風輕ζ 提交于 2019-12-01 18:03:09
If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work? struct Foo { int x; }; struct Bar : public Foo { }; template<class T, int T::* z> struct Buzz { }; static int Bar::* const workaround = &Foo::x; int main() { // This works. Downcasting of pointer to members in general is fine. int Bar::* y = &Foo::x; // But this doesn't, at least in G++ 4.2 or Sun C++ 5.9. Why

Cast Any to Float always fails in swift4.1

守給你的承諾、 提交于 2019-12-01 15:00:40
问题 In the former version, to get a float value from a [String: Any] dictionary, I can use let float = dict["somekey"] as? Float , but in swift4.1, it doesn't work. It seems the type of dict["somekey"] has been implicitly inferred as Double before I get it, so casting from Double to Float always fails. I wonder if it is a new characteristic or just a bug. --Here is the update. I re-downloadeded an Xcode9.2 and did some experiments, now I think I figure out what's going on. Here is the test code:

Swig downcasting from Base* to Derived*

﹥>﹥吖頭↗ 提交于 2019-12-01 10:58:18
I have the following c++ classes (simplified) which I am exposing to Python using SWIG: struct Component { virtual void update(); } struct DerivedComponent : public Component { void update() { cout << "DerivedComponent::update()" << endl; } void speak() { cout << "DerivedComponent::speak()" << endl; } } class Entity { public: Component* component(const std::string& class_name) { return m_components[class_name]; } private: std::unordered_map<std::string, Component*> m_components; } Now, in Python I can successfully call component("DerivedComponent").update() on an Entity instance. However, I