Questions tagged [dynamic-cast]

The dynamic_cast conversion allows for safely converting pointers (and references) to classes up, down, and sideways in the inheritance hierarchy.

From cppreference.com:

Safely converts pointers and references to classes up, down, and sideways along the inheritance hierarchy.

dynamic_cast has usage scenarios and limitations. In general terms this amounts to it being valid for casting polymorphic types and it performs run-times checks to ensure that the result is valid for the requested object type.

621 questions
15
votes
4 answers

Comma operator with typeid

I was studying A Generic Non-intrusive Smart Pointer Implementation. I have some confusion in section 4. One statement is the expression supplied as the argument to the typeid operator is only evaluated if the result type is an lvalue of…
Rakib
  • 7,435
  • 7
  • 29
  • 45
15
votes
2 answers

dyn_cast vs. dynamic_cast in C++

I come across a lot of dyn_cast in a codebase I am working on. Is it the same thing as dynamic_cast ? or something different ? I searched a bit but couldn't find much info..
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169
14
votes
4 answers

`dynamic_cast` from Base to Derived

Yes, I know that downcast using dynamic_cast can't compile if the Base is not polymorphic, but my problem is not about this. class Base { public: virtual void bar() { cout << "bar\n"; } }; class Derived:…
Alcott
  • 17,905
  • 32
  • 116
  • 173
13
votes
4 answers

Why virtual function call is faster than dynamic_cast?

I wrote a simple example, which estimates average time of calling virtual function, using base class interface and dynamic_cast and call of non-virtual function. Here is it: #include #include #include #include…
D_E
  • 1,196
  • 11
  • 24
13
votes
5 answers

Real world example of dynamic_cast in C++

Can anybody give me a real world example of a case when dynamic_cast is needed and can't be worked around at all? Examples I can think of can generally be worked around with double dispatch. If the constraint is too strong, an example where…
Russell
  • 3,975
  • 7
  • 37
  • 47
13
votes
2 answers

C++: Comparing pointers of base and derived classes

I'd like some information about best practices when comparing pointers in cases such as this one: class Base { }; class Derived : public Base { }; Derived* d = new Derived; Base* b = dynamic_cast(d); // When comparing the two pointers…
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
13
votes
4 answers

C++ dynamic_cast - polymorphic requirement and downcasting

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…
Mahesh
  • 34,573
  • 20
  • 89
  • 115
13
votes
1 answer

Why don't C++ compilers optimize this dynamic_cast from a final class?

Consider this class hierarchy: struct Animal { virtual ~Animal(); }; struct Cat : virtual Animal {}; struct Dog final : virtual Animal {}; My understanding is that putting final on class Dog ensures that nobody can ever create a class inheriting…
Quuxplusone
  • 23,928
  • 8
  • 94
  • 159
13
votes
2 answers

cast const Class using dynamic_cast

I want to cast this: class Base { public: virtual ~Base(){}; }; class Der : public Base {}; int main() { const Base* base = new Der; Der* der = dynamic_cast(base); // Error return 0; } What should I do? I tried to put:…
okami
  • 2,093
  • 7
  • 28
  • 40
13
votes
4 answers

dynamic_cast fails when used with dlopen/dlsym

Intro Let me apologise upfront for the long question. It is as short as I could make it, which is, unfortunately, not very short. Setup I have defined two interfaces, A and B: class A // An interface { public: virtual ~A() {} virtual void…
Kees-Jan
  • 518
  • 4
  • 16
13
votes
1 answer

Smart pointers and dynamic_cast

I apologize in advance if this was answered already as I looked and could not find the answer. NOTE: this IS a homework assignment, so if you feel uncomfortable answering, I completely understand. I have the following: ptr.h: template
mlnyc
  • 2,636
  • 2
  • 24
  • 29
12
votes
6 answers

java: combined instanceof and cast?

(Please no advise that I should abstract X more and add another method to it.) In C++, when I have a variable x of type X* and I want to do something specific if it is also of type Y* (Y being a subclass of X), I am writing this: if(Y* y =…
Albert
  • 65,406
  • 61
  • 242
  • 386
12
votes
6 answers

In C++ check if two instances of a base class are infact of the same subclass

The below code explains the problem. Fill in same_sub_class to detect if the two pointers to virtual base class A are in fact the same concrete class. struct A { ... }: struct B : public A { ... }: struct C : public A { ... } bool…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
12
votes
1 answer

What is the meaning of `*dynamic_cast(...)`?

Recently I was looking in the code of an open source project, and I saw a bunch of statements of the form T & object = *dynamic_cast(ptr);. (Actually this was occuring in macro used to declare many functions following a similar pattern.) To me…
Chris Beck
  • 15,614
  • 4
  • 51
  • 87
12
votes
3 answers

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

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) { …
Eric
  • 2,029
  • 2
  • 26
  • 36
1 2
3
41 42