downcast

Is it really downcasting not possible? It is working fine for me

て烟熏妆下的殇ゞ 提交于 2019-12-11 13:16:59
问题 I know there are already some questions posted related to this same topic, but I have seen different answers so I am quite confused on which answer is correct. On the below link, people mentioned that downcasting is not possible Convert/Cast base type to Derived type While on this link below, people mentioned that downcasting is possible only if the derived class is an instance of the base class downcast and upcast I did a little experiment and implemented the downcasting on a mini project(C#

How to avoid down-casting when return types are not known at compile time?

送分小仙女□ 提交于 2019-12-11 09:54:37
问题 Suppose I have a abstract base class called Node . class Node { public: Node() { leftChild = NULL; rightChild = NULL; }; Node * leftChild, *rightChild; void attach(Node * LC, Node * RC) { leftChild = LC; rightChild = RC; }; }; I also have multiple functions (of which I'll include two for simplicity but in reality this could be any number). float add(float a, float b){return a+b;} bool gt(float a, float b){return a>b;} For each function there is an associated class. The first is as follows.

String to Object typecasting - Difference

回眸只為那壹抹淺笑 提交于 2019-12-11 04:48:52
问题 What is the difference between. public class Test { public static void main(String args[]) { String toBeCast = "cast this string"; A a = toBeCast; // error - Type mismatch: cannot convert from String to A Object object = toBeCast; } } public class A { } When we say every object extends Object class , why does A a = toBeCast; not allowed, but this Object object = toBeCast; works fine. 回答1: Remember that old saying from geometry class - "Every square is a rectangle, but not every rectangle is a

Why isn't automatic downcasting applied to template functions?

北战南征 提交于 2019-12-08 18:48:48
问题 Someone asked this question about string appending. It's string s; s = s + 2; not compiling. People gave answers stating that operator+ is defined as a template function while operator+= is not, so auto downcasting ( int(2) to char(2) ) is not applied. The prototypes are template<typename _CharT, typename _Traits, typename _Alloc> class basic_string{ basic_string& operator+=(_CharT __c); }; template<typename _CharT, typename _Traits, typename _Alloc> inline basic_string<_CharT, _Traits,

Is it possible to downcast an object to a subclass which does not define extra variable or vtable in C++?

青春壹個敷衍的年華 提交于 2019-12-08 15:58:22
问题 Is it possible to downcast an object to a subclass does not define any extra variable or virtual method? If I have these classes, class A { public: A (); }; class B : public A { public: void method1 () {} B (); }; is this (1) possible and (2) safe by standard? A* a = new A (); B* b = (B*)a; b->method1(); 回答1: The pointer conversion acts as a static_cast . 5.2.9/2 says, If the object of type [ A ] is actually a subobject of an object of type [ B ], the result refers to the enclosing object of

Swift's pow() function won't accept Doubles as arguments

不羁的心 提交于 2019-12-07 13:37:22
问题 I created this infix operator ^^ as a substitute to using the pow function: infix operator ^^ { associativity left precedence 155 } func ^^ <T: IntegerLiteralConvertible>(left: T, right: T) -> T { return pow(left as Double, right as Double) } I used the IntegerLiteralConvertible protocol as a type constraint for the generics left and right , because from my understanding this diagramm shows, that it basically includes all number types. In order to use the pow function I have to downcast left

How to downcast from non-polymorphic virtual base class?

坚强是说给别人听的谎言 提交于 2019-12-07 05:21:45
问题 Is there a way to downcast from a virtual base class to a derived class when there are no virtual functions involved? Here's some code to demonstrate what I'm talking about: struct Base1 { int data; }; struct Base2 { char odd_size[9]; }; struct ViBase { double value; }; struct MostDerived : Base1, Base2, virtual ViBase { bool ok; }; void foo(ViBase &v) { MostDerived &md = somehow_cast<MostDerived&>(v); //but HOW? md.ok = true; } int main() { MostDerived md; foo(md); } Please note that the

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

落爺英雄遲暮 提交于 2019-12-06 09:22:31
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()); } int getI()const {return v->getI();} void initialize() {v=new Handler(10);} void hi() {v->hi();}

How to downcast from non-polymorphic virtual base class?

天大地大妈咪最大 提交于 2019-12-05 09:02:57
Is there a way to downcast from a virtual base class to a derived class when there are no virtual functions involved? Here's some code to demonstrate what I'm talking about: struct Base1 { int data; }; struct Base2 { char odd_size[9]; }; struct ViBase { double value; }; struct MostDerived : Base1, Base2, virtual ViBase { bool ok; }; void foo(ViBase &v) { MostDerived &md = somehow_cast<MostDerived&>(v); //but HOW? md.ok = true; } int main() { MostDerived md; foo(md); } Please note that the code is for demonstration only. My real scenario is fairly complex and involves template parameters and

How to avoid downcast?

爷,独闯天下 提交于 2019-12-05 05:09:07
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 queue (so upcasting takes place here...). I know that downcasting is a sign of a bad design but is it