dynamic-cast

When is static cast safe when you are using multiple inheritance?

家住魔仙堡 提交于 2019-12-17 19:34:38
问题 I found myself in a situation where I know what type something is. The Type is one of three (or more) levels of inheritance. I call factory which returns B* however T is either the highest level of a type (if my code knows what it is) or the 2nd level. Anyways, I did a static_cast in the template which is the wrong thing to do. My question is WHEN can I static cast safely? Is there ever such a time? I did it in this case because I'd rather get compile errors when I accidentally have T as

Why can't I dynamic_cast “sideways” during multiple inheritence?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 17:10:08
问题 The following code throws std::bad_cast struct Foo { void foo () {} }; struct Bar { Bar () { dynamic_cast <Foo &> (*this) .foo (); } virtual ~ Bar () {} }; struct Baz : public Foo, public Bar { }; int main () { Baz b; } I remember once reading how dynamic_cast has implementation performance trade-offs because "it traverses the full inheritence lattice" in order to evaluate correctly. What the compiler needs to do here is first cast up and then down again. Is it possible to make the above work

dynamic_cast of “this” inside constructor

强颜欢笑 提交于 2019-12-17 16:37:48
问题 This question is very similar to this one Why can't I dynamic_cast "sideways" during multiple inheritence?, except that the cast does work - just not inside in the constructor. Header: class A { public: virtual ~A() {} void printA(); }; class B { public: B(); virtual ~B() {} void printB(); private: std::string message_; }; class C : public A, public B { public: C() {} virtual ~C() {} }; Source: void A::printA() { cout << "A" << endl; } B::B() { A* a = dynamic_cast< A* >( this ); if ( a ) {

Is there a way to do dynamic implicit type casting in C#?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 16:28:02
问题 Given this class with an implicit cast operator: public class MyDateTime { public static implicit operator MyDateTime(System.Int64 encoded) { return new MyDateTime(encoded); } public MyDateTime(System.Int64 encoded) { _encoded = encoded; } System.Int64 _encoded; } I can now do the following: long a = 5; MyDateTime b = a; But NOT the following: long f = 5; object g = f; MyDateTime h = g; This gives a compile time: Cannot implicitly convert type 'object' to 'MyDateTime'. Makes sense to me. Now

dynamic_cast and static_cast in C++

筅森魡賤 提交于 2019-12-17 00:21:13
问题 I am quite confused with the dynamic_cast keyword in C++. struct A { virtual void f() { } }; struct B : public A { }; struct C { }; void f () { A a; B b; A* ap = &b; B* b1 = dynamic_cast<B*> (&a); // NULL, because 'a' is not a 'B' B* b2 = dynamic_cast<B*> (ap); // 'b' C* c = dynamic_cast<C*> (ap); // NULL. A& ar = dynamic_cast<A&> (*ap); // Ok. B& br = dynamic_cast<B&> (*ap); // Ok. C& cr = dynamic_cast<C&> (*ap); // std::bad_cast } the definition says: The dynamic_cast keyword casts a datum

dynamic_cast and static_cast in C++

浪子不回头ぞ 提交于 2019-12-17 00:21:03
问题 I am quite confused with the dynamic_cast keyword in C++. struct A { virtual void f() { } }; struct B : public A { }; struct C { }; void f () { A a; B b; A* ap = &b; B* b1 = dynamic_cast<B*> (&a); // NULL, because 'a' is not a 'B' B* b2 = dynamic_cast<B*> (ap); // 'b' C* c = dynamic_cast<C*> (ap); // NULL. A& ar = dynamic_cast<A&> (*ap); // Ok. B& br = dynamic_cast<B&> (*ap); // Ok. C& cr = dynamic_cast<C&> (*ap); // std::bad_cast } the definition says: The dynamic_cast keyword casts a datum

dynamic_cast to the same type does not check type of object

為{幸葍}努か 提交于 2019-12-14 02:47:42
问题 I am trying to determine whether an object pointed by a T* pointer is truly a T object, or some other, unrelated type. I tried dynamic_cast, however it is less than useless, it returns the pointer itself instead of null even when it is obvious it does not point to a valid T object: Object* garbage = reinterpret_cast<Object*>(0x12345678); if( dynamic_cast<Object*>(garbage) == NULL ){ cout << "Expected behaviour (by me)" << endl; }else{ cout << "You've got to be kidding me" << endl; } Is there

C++ dynamic_cast vs storing object type in a static enum?

偶尔善良 提交于 2019-12-12 18:27:24
问题 I am developing a big hierarchy of classes for a framework that will require quite a lot of type casting when it gets done. My question is, how stupid of an idea is to put in a static member that uses an enum to store all the object types in the hierarchy. Having the member static for every class will not increase the instanced object sizes, and will give a (potentially) faster way to determined the type of an object during runtime than dynamic_cast. At least that's the basic idea. How

Is the c++ primer making something wrong with the usage of `dynamic_cast`?

喜你入骨 提交于 2019-12-12 17:27:06
问题 Quoted from C++ Primer 5th 19.2.1. The dynamic_cast Operator A dynamic_cast has the following form: dynamic_cast<type*>(e) dynamic_cast<type&>(e) dynamic_cast<type&&>(e) where type must be a class type and (ordinarily) names a class that has virtual functions. In the first case, e must be a valid pointer (§ 2.3.2, p. 52); in the second, e must be an lvalue; and in the third, e must not be an lvalue. In all cases, the type of e must be either a class type that is publicly derived from the

Dynamic cast using Type of object C#

旧城冷巷雨未停 提交于 2019-12-12 11:00:28
问题 I have one abstract class named A, and other classes (B, C, D, E, ...) that implements A I also have a list of A objects. I'd like to be able to cast dynamicly each of the object in that list to their "base" type (ie B, C, D, ...) to be able to call their constructor in an other method. Here is what I have done for now : abstract class A { } class B : A { } class C : A { } class D : A { } class E : A { } // ... class Program { static void Main(string[] args) { List<A> list = new List<A> { new