downcast

Swig downcasting from Base* to Derived*

走远了吗. 提交于 2019-12-01 07:49:59
问题 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

How can I downcast to class' type E or at least make it in a safe way without warnings?

五迷三道 提交于 2019-12-01 06:39:15
问题 I have super abstract class Node and 50 types of subclasses SubNode. I have a generic Class <E extends Node> which has a private var List<E> and a method which unfortunately has to accept superclass Node ALWAYS, cannot move it to just E: public void addSubElement (Node node){ if (node instanceOf E) subElements.add((E)node); else //Doing extra steps for occasional non-E nodes like discarding them silently without CastException; } Any solution (Reflection?) able to compile without warnings,

Is the performance/memory benefit of short nullified by downcasting?

自作多情 提交于 2019-12-01 05:22:26
I'm writing a large scale application where I'm trying to conserve as much memory as possible as well as boost performance. As such, when I have a field that I know is only going to have values from 0 - 10 or from -100 - 100, I try to use the short data type instead of int . What this means for the rest of the code, though, is that all over the place when I call these functions, I have to downcast simple int s into short s. For example: Method Signature public void coordinates(short x, short y) ... Method Call obj.coordinates((short) 1, (short) 2); It's like that all throughout my code because

Is the performance/memory benefit of short nullified by downcasting?

雨燕双飞 提交于 2019-12-01 02:28:18
问题 I'm writing a large scale application where I'm trying to conserve as much memory as possible as well as boost performance. As such, when I have a field that I know is only going to have values from 0 - 10 or from -100 - 100, I try to use the short data type instead of int . What this means for the rest of the code, though, is that all over the place when I call these functions, I have to downcast simple int s into short s. For example: Method Signature public void coordinates(short x, short

Dynamic downcast on private inheritance within private scope

笑着哭i 提交于 2019-12-01 02:21:39
A tweak on this question that I've run into. Consider: class A {}; class B : private A { static void foo(); }; void B::foo(){ B* bPtr1 = new B; A* aPtr1 = dynamic_cast<A*>(bPtr1); // gives pointer B* bPtr2 = dynamic_cast<B*>(aPtr1); // gives NULL } Since aPtr1 is, in fact, of type B* , and since we've got full access to B and its inheritance from A , I'd expected both casts to work. But they don't; why? Is there another way to achieve this cast? Note that: If foo() were not a member of B, both casts would fail. If B inherits from A publicly, both casts would work. 5.2.7 (ISO/IEC 14882, 12/29

Dynamic downcast on private inheritance within private scope

柔情痞子 提交于 2019-11-30 22:43:17
问题 A tweak on this question that I've run into. Consider: class A {}; class B : private A { static void foo(); }; void B::foo(){ B* bPtr1 = new B; A* aPtr1 = dynamic_cast<A*>(bPtr1); // gives pointer B* bPtr2 = dynamic_cast<B*>(aPtr1); // gives NULL } Since aPtr1 is, in fact, of type B* , and since we've got full access to B and its inheritance from A , I'd expected both casts to work. But they don't; why? Is there another way to achieve this cast? Note that: If foo() were not a member of B,

Safety of invalid downcast using static_cast (or reinterpret_cast) for inheritance without added members

為{幸葍}努か 提交于 2019-11-30 18:55:14
问题 I was wondering what the standard says about the safety of the following code: class A { int v; }; class B: public A { }; // no added data member A a; B& b = static_cast<B&>(a); Obviously the runtime type of a is A , not B , so the cast is not really type safe. However, since there was no member added and nothing is virtual, IMO the memory layout of the classes should be the same and this should work (maybe it would be nicer to write reinterpret_cast to indicate this behaviour?). My guess

C++ dynamic_cast - polymorphic requirement and downcasting

故事扮演 提交于 2019-11-30 09:10:55
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 { public: base() { cout<< " \n base constructor \n"; } }; class derived : public base { public: derived() {

How to downcast a Java object?

南楼画角 提交于 2019-11-30 00:15:42
I am trying to understand Java's polymorphism, and I have one question about downcasting an object. Let's say for this example I have two subclasses Dog and Cat that inherit from a superclass Animal From what I understood, the only way to downcast an object is if this Object is already of the good type, like this: Animal a = new Dog(); Dog d = (Dog) a; This works right? But what if I want to create a regular animal without knowing what it would be, and then cast it when I know, how can I do that? Animal a = new Animal(); Dog d = (Dog) a; This will throw a ClassCastException at runtime right?

How does one downcast a std::shared_ptr?

大城市里の小女人 提交于 2019-11-29 20:23:38
Consider: struct SomethingThatsABase { virtual bool IsChildOne() const { return false; } virtual bool IsChildTwo() const { return false; } }; struct ChildOne : public SomethingThatsABase { virtual bool IsChildOne() const { return true; } }; struct ChildTwo : public SomethingThatsABase { virtual bool IsChildTwo() const { return true; } }; void SomeClientExpectingAChildOne(std::shared_ptr<ChildOne> const& ptrOne) { //Does stuff } void SomeClient(std::shared_ptr<SomethingThatsABase> const& ptr) { if (ptr->IsChildOne()) { SomeClientExpectingAChildOne(ptr); //Oops. //Hmm.. can't static_cast here,