downcast

Trying to understand how the casting/conversion is done by compiler,e.g., when cast from float to int

a 夏天 提交于 2019-12-25 09:42:28
问题 When a float is casted to int, how this casting is implemented by compiler. Does compiler masks some part of memory of float variable i.e., which part of memory is plunked by compiler to pass the remaining to int variable. I guess the answer to this lies in how the int and float is maintained in memory. But isn't it machine dependent rather than compiler dependent. How compiler decides which part of memory to copy when casted to lower type (this is a static casting, right). I am kind of

Call a method that requires a derived class instance typed as base class in VB.NET or C#

孤者浪人 提交于 2019-12-25 05:23:09
问题 I have two objects - "Spaceship" and "Planet" derived from a base "Obj". I have defined several classes - Circle, Triangle, Rectangle, etc. which all inherit from a "Shape" Class. For collision detection purposes, I want to give Obj a "shape": Dim MyShape as Shape So that in "Spaceship" I can: MyShape = new Triangle(blah,blah) and in "Planet" I can: MyShape = new Circle(blah,blah) I have a method (overloaded several times) which checks for collisions between different shapes, for example:

Cleanest way to fix this castings behavior

落花浮王杯 提交于 2019-12-25 05:18:18
问题 Imagine I have a list with 50 different type of a certain subclasses of Node which I expect to be the same type or get a ClassException if not. I have a method which receives this list and a node holding and expeting a certain type I would like to do something like this: public <E extends Node> receive(List<Node> list, Node<E extends Node> node){ for (Node element : list ){ node.addElement((E) element); //Type erasure, if you put wrong node no CastException in Runtime } } but avoid doing this

C++ - faster downcasting children of a tree-node?

∥☆過路亽.° 提交于 2019-12-25 01:46:11
问题 I have a simple hierarchy tree structure with a base class Node representing a node. A node could be of another specific type (subclassing). class Node { vector<Node*> childs; // simple node manipulation methods const vector<Node*>& getChildren() { return childs; } } and I have a couple of subclasses of Node : class FacultyNode : public Node; ... class DepartmentNode : public Node; ... Say I know that all children of a faculty node is of DepartmentNode type, to save the developer's work I

problem with a HashSet's Iterator

橙三吉。 提交于 2019-12-24 09:39:50
问题 I'm trying to see if HashSet would be the solution for my next project so i'm doing some very easy test to check functionalities. I have a simple class Klant : public class Klant { private int klantNummer; public Klant(int nummer) { this.klantNummer = nummer; } public int getKlantNummer() { return this.klantNummer; } } and a class with through composition uses a HashSet public class MySet<Klant> { private Collection<Klant> mySet = null; public MySet() { mySet=new HashSet<Klant>(); } public

Cant copy construction be done without creating an explicit function in the pure virtual base class?

和自甴很熟 提交于 2019-12-22 18:32:36
问题 My objective is to do a deep copy of a class, but a virtual class is causing trouble. #include<iostream> using namespace std; class Vir//pure virtual class { public: virtual void hi()=0; }; class Handler:public Vir { public: int i; Handler() {} Handler(int val):i(val) {} void hi() {cout<<"Value of i="<<i<<endl;} int getI() const {return i;} void setI(int j) {i=j;} }; class ControlPanel { public: Vir *v; ControlPanel(const ControlPanel& c)//copy constructor { v=new Handler; v->setI(c.getI());

How to avoid downcast?

允我心安 提交于 2019-12-22 05:07:14
问题 I have an implementation of a State Pattern where each state handles events it gets from a event queue. Base State class therefore has a pure virtual method void handleEvent(const Event*) . Events inherit base Event class but each event contains its data that can be of a different type (e.g. int, string...or whatever). handleEvent has to determine the runtime type of the received event and then perform downcast in order to extract event data. Events are dynamically created and stored in a

Downcasting a list of objects in C#

ぃ、小莉子 提交于 2019-12-21 09:59:21
问题 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 :

wrong generic type in swift

谁说胖子不能爱 提交于 2019-12-21 05:56:49
问题 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

Downcasting from base pointer to templated derived types

*爱你&永不变心* 提交于 2019-12-20 09:25: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