dynamic-cast

Swift dynamicCast error while inserting new object to database

一世执手 提交于 2019-12-22 12:46:15
问题 I have a dictionary which I have all data that I want to insert into database as new object. The problem is when I try to cast the newly created object it gives me exception in: libswift_stdlib_core.dylib`swift_dynamicCast: part of assembly code. The code that i am using is like this: var group:Group if (array.count == 0) { group = NSEntityDescription.insertNewObjectForEntityForName("Group", inManagedObjectContext:appDelegate.managedObjectContext) as Group } and the structure of Group class

dynamic cast with interfaces

那年仲夏 提交于 2019-12-21 04:34:11
问题 I have a class with implements 2 interfaces and inherits 1 class. So, generally it looks like this: class T : public A, public IB, public IC { }; There is one point in the code where I have an IB * , but could really use an A * . I was hoping that a dynamic cast would like this: IB *b_ptr = new T; // it's really more complicated, but serves the example A *a_ptr = dynamic_cast<A *>(b_ptr); unfortunately, this doesn't work. Is there a proper way to do this? Or should I implement a work around?

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

dynamic_cast vs exposing virtual functions in parent class (C++)

删除回忆录丶 提交于 2019-12-20 01:45:12
问题 I have a parent class "base" and another class "derived" that inherits from "base". "derived" has 1 method cH1. if I do this: base* b = new derived(); And I want to be able to do this: b->cH1(); Obviously I can't and there are 2 solutions: Either declare cH1 as pure virtual in base. or do this: dynamic_cast<derived*>(b)->cH1(); Which one is a better practice? 回答1: If cH1 method semantically applies to base , then make it a base 's method. Else, leave cH1 to derived , and use dynamic_cast . I

dynamic_cast returns NULL but it shouldn't

喜你入骨 提交于 2019-12-19 19:57:55
问题 I'm having the following class hierarchy: class IStorage { [...] } Q_DECLARE_INTERFACE(IStorage, "ch.gorrion.smssender.IStorage/1.0") class ISQLiteStorage: public IStorage { Q_INTERFACES(IStorage) [...] } Q_DECLARE_INTERFACE(ISQLiteStorage, "ch.gorrion.smssender.ISQLiteStorage/1.0") class DASQLiteStorage: public QObject, public ISQLiteStorage { Q_OBJECT Q_INTERFACES(ISQLiteStorage) [...] } I'm using QT and am trying to create a plugin (for my app) with QtPlugin. I'm creating an instance of

Safely checking the type of a variable

醉酒当歌 提交于 2019-12-19 11:32:15
问题 For a system I need to convert a pointer to a long then the long back to the pointer type. As you can guess this is very unsafe. What I wanted to do is use dynamic_cast to do the conversion so if I mixed them I'll get a null pointer. This page says http://publib.boulder.ibm.com/infocenter/lnxpcomp/v7v91/index.jsp?topic=/com.ibm.vacpp7l.doc/language/ref/clrc05keyword_dynamic_cast.htm The dynamic_cast operator performs type conversions at run time. The dynamic_cast operator guarantees the

How to write own dynamic_cast

主宰稳场 提交于 2019-12-19 08:13:55
问题 This have been asked in the interview. How to write own dynamic_cast. I think, on the basis of typeid's name function. Now how to implement own typid? I have no clue on it. 回答1: There is a reason you don't have any clue, dynamic_cast and static_cast are not like const_cast or reinterpret_cast , they actually perform pointer arithmetic and are somewhat typesafe. The pointer arithmetic In order to illustrate this, think of the following design: struct Base1 { virtual ~Base1(); char a; }; struct

Cannot dynamic_cast void* to templated class

↘锁芯ラ 提交于 2019-12-19 04:58:34
问题 The exact error I'm getting is: Cannot dynamic_cast 'object' (of type 'void*') to type 'class udDator(int)*' (source is not a pointer to a class) This is happening inside an overridden operator delete. I'm attempting to create a templated memory management class that can inherit into any other class, managing memory through references. This would be in place of something like a smart shared_ptr, in an attempt to make memory management even more invisible, and without extra typing ( shared_ptr

Why does protected inheritance cause dynamic_cast to fail?

守給你的承諾、 提交于 2019-12-18 03:46:08
问题 I changed my C++ base class to be protected inheritance and my dynamic_cast (s) stopped working. Why should changing the inheritance to protected change the behavior of dynamic_cast ? struct Base { static Base *lookupDerived(); // Actually returns a Derived * object. }; struct Derived : protected /* Switch this to public to get it working */ Base { static void test() { Base *base = lookupDerived(); if (dynamic_cast<Derived *>(base)) { std::cout << "It worked (we must be using public

How is dynamic_cast implemented

情到浓时终转凉″ 提交于 2019-12-17 22:37:53
问题 Consider this simple hierarchy: class Base { public: virtual ~Base() { } }; class Derived : public Base { }; Trying to downcast Base* p to Derived* is possible using dynamic_cast<Derived*>(p) . I used to think dynamic_cast works by comparing the vtable pointer in p to the one in a Derived object. But what if we derive another class from Derived ? We now have: class Derived2 : public Derived { }; In this case: Base* base = new Derived2; Derived* derived = dynamic_cast<Derived*>(base); We still