downcast

Why is DECLARE_DYNAMIC & IMPLEMENT_DYNAMIC nessary for DYNAMIC_DOWNCAST?

强颜欢笑 提交于 2019-11-29 17:06:56
I have two classes: /*Switch.h*/ class CSwitch : public CDeviceEntity {} /*EndSystem.h*/ class CEndSystem : public CDeviceEntity {} but when I use: CDeviceEntity* dev = NULL; dev = topo->headList[i]->node; if ( DYNAMIC_DOWNCAST( CEndSystem, dev ) != NULL ) {} " DYNAMIC_DOWNCAST " always returns not NULL while dev is kind of class CEndSystem or class CSwitch . If use: /*Switch.h*/ class CSwitch : public CDeviceEntity { DECLARE_DYNAMIC(CSwitch) } and /*Switch.cpp*/ IMPLEMENT_DYNAMIC(CSwitch, CDeviceEntity) /*EndSystem.h*/ class CEndSystem : public CDeviceEntity { DECLARE_DYNAMIC(CEndSystem) }

How to properly downcast in C# with a SWIG generated interface?

瘦欲@ 提交于 2019-11-29 08:32:14
I've got a very large and mature C++ code base that I'm trying to use SWIG on to generate a C# interface for. I cannot change the actual C++ code itself but we can use whatever SWIG offers in the way of extending/updating it. I'm facing an issue where a C++ function that is written as below is causing issues in C#. A* SomeClass::next(A*) The caller might do something like: A* acurr = 0; while( (acurr = sc->next(acurr)) != 0 ){ if( acurr isoftype B ){ B* b = (B*)a; ...do some stuff with b.. } elseif( acurr isoftype C ) ... } Essentially, iterating through a container of elements that, depending

Downcasting in Swift with as and as?

折月煮酒 提交于 2019-11-29 03:32:34
What's the difference between these two code snippets: let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as UITableViewCell? // vs let cell = tableView.dequeueReusableCellWithIdentifier("cellId") as? UITableViewCell Isn't the result exactly the same? In that code there's no difference, in both cases it evaluates to UITableViewCell? The real difference is: in the first case a downcast to UITableViewCell? is expected to always succeed (even if it's nil), so if dequeueReusableCellWithIdentifier returns something that's not an instance of UITableViewCell (or an instance of a class

protocol typed array can't be downcast to concrete type array

蹲街弑〆低调 提交于 2019-11-28 21:26:45
protocol P : class { var value:Int {get} } class X : P { var value = 0 init(_ value:Int) { self.value = value } } var ps:[P] = [X(1), X(2)] for p in ps { if let x = p as? X { // works for a single variable ... } } if let xs = ps as? [X] { // doesn't work for an array (EXC_BAD_ACCESS) ... } If P is a class instead of a protocol, than the code works correctly. What's the difference between class and protocol? They're both implemented as pointers in the heap, aren't they? The above code can be compiled successfully, but crash at runtime. What does this EXC_BAD_ACCESS error mean? Thanks to

How does one downcast a std::shared_ptr?

老子叫甜甜 提交于 2019-11-28 16:10:46
问题 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)

In Objective-C, what is the equivalent of Java's “instanceof” keyword?

余生颓废 提交于 2019-11-28 15:21:37
I would like to check whether an object (e.g. someObject ) is assignable (cast-able) to a variable of another type (e.g. SpecifiedType ). In Java, I can write: someObject instanceof SpecifiedType A related question is finding whether the runtime type of an object is equal to a another type. In Java, I can write: someObject.getClass().equals(SpecifiedType.class) How can this be done in Objective-C? mouviciel Try [myObject class] for returning the class of an object. You can make exact comparisons with: if ([myObject class] == [MyClass class]) but not by using directly MyClass identifier.

Why is DECLARE_DYNAMIC & IMPLEMENT_DYNAMIC nessary for DYNAMIC_DOWNCAST?

旧城冷巷雨未停 提交于 2019-11-28 10:48:30
问题 I have two classes: /*Switch.h*/ class CSwitch : public CDeviceEntity {} /*EndSystem.h*/ class CEndSystem : public CDeviceEntity {} but when I use: CDeviceEntity* dev = NULL; dev = topo->headList[i]->node; if ( DYNAMIC_DOWNCAST( CEndSystem, dev ) != NULL ) {} " DYNAMIC_DOWNCAST " always returns not NULL while dev is kind of class CEndSystem or class CSwitch . If use: /*Switch.h*/ class CSwitch : public CDeviceEntity { DECLARE_DYNAMIC(CSwitch) } and /*Switch.cpp*/ IMPLEMENT_DYNAMIC(CSwitch,

C++ inheritance downcasting

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:53:01
I have my base class as follows: class point //concrete class { ... //implementation } class subpoint : public point //concrete class { ... //implementation } How do I cast from a point object to a subpoint object? I have tried all three of the following: point a; subpoint* b = dynamic_cast<subpoint*>(&a); subpoint* b = (subpoint*)a; subpoint b = (subpoint)a; What is wrong with these casts? How do I cast from a point object to a subpoint object? You can't; unless either point has a conversion operator, or subpoint has a conversion constructor, in which case the object types can be converted

How to properly downcast in C# with a SWIG generated interface?

邮差的信 提交于 2019-11-28 02:02:59
问题 I've got a very large and mature C++ code base that I'm trying to use SWIG on to generate a C# interface for. I cannot change the actual C++ code itself but we can use whatever SWIG offers in the way of extending/updating it. I'm facing an issue where a C++ function that is written as below is causing issues in C#. A* SomeClass::next(A*) The caller might do something like: A* acurr = 0; while( (acurr = sc->next(acurr)) != 0 ){ if( acurr isoftype B ){ B* b = (B*)a; ...do some stuff with b.. }

In Objective-C, what is the equivalent of Java's “instanceof” keyword?

做~自己de王妃 提交于 2019-11-27 19:45:05
问题 I would like to check whether an object (e.g. someObject ) is assignable (cast-able) to a variable of another type (e.g. SpecifiedType ). In Java, I can write: someObject instanceof SpecifiedType A related question is finding whether the runtime type of an object is equal to a another type. In Java, I can write: someObject.getClass().equals(SpecifiedType.class) How can this be done in Objective-C? 回答1: Try [myObject class] for returning the class of an object. You can make exact comparisons