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
179
votes
10 answers

dynamic_cast and static_cast in C++

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 (&a); // NULL, because 'a' is not a…
Vijay
  • 65,327
  • 90
  • 227
  • 319
101
votes
13 answers

java: How can I do dynamic casting of a variable from one type to another?

I would like to do dynamic casting for a Java variable, the casting type is stored in a different variable. This is the regular casting: String a = (String) 5; This is what I want: String theType = 'String'; String a = (theType) 5; Is this…
ufk
  • 30,912
  • 70
  • 235
  • 386
77
votes
7 answers

Are there practical uses for dynamic-casting to void pointer?

In C++, the T q = dynamic_cast(p); construction performs a runtime cast of a pointer p to some other pointer type T that must appear in the inheritance hierarchy of the dynamic type of *p in order to succeed. That is all fine and well. However,…
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
76
votes
3 answers

Portably safe to pass NULL/zero to dynamic_cast?

Out of habit for checking null pointers, I have sometimes written: MyClass * c = someBasePtr ? dynamic_cast(someBasePtr) : 0; if (c) {... In effect, checking for a null pointer before passing to dynamic cast, and also checking the…
sdg
  • 4,645
  • 3
  • 32
  • 26
61
votes
4 answers

Difference in behavior while using dynamic_cast with reference and pointers

I was checking the behavior of dynamic_cast and found that when it fails, std::bad_cast exception is thrown only if the destination is a reference type. If the destination is a pointer type then no exception is thrown from the cast. This is my…
Naveen
  • 74,600
  • 47
  • 176
  • 233
52
votes
6 answers

Performance of dynamic_cast?

Before reading the question: This question is not about how useful it is to use dynamic_cast. Its just about its performance. I've recently developed a design where dynamic_cast is used a lot. When discussing it with co-workers almost everyone says…
MOnsDaR
  • 8,401
  • 8
  • 49
  • 70
48
votes
3 answers

How is dynamic_cast implemented

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(p). I used to think dynamic_cast works by comparing the…
Avidan Borisov
  • 3,235
  • 4
  • 24
  • 27
47
votes
4 answers

How to perform a dynamic_cast with a unique_ptr?

I have a class hierarchy as follows: class BaseSession : public boost::enable_shared_from_this class DerivedSessionA : public BaseSession class DerivedSessionB : public BaseSession Within the derived class functions, I regularly call…
Sharath
  • 1,627
  • 2
  • 18
  • 34
46
votes
6 answers

dynamic_cast from "void *"

According to this, void* has no RTTI information, therefore casting from void* is not legal and it make sense. If I remember correctly, dynamic_cast from void* was working on gcc. Can you please clarify the issue.
dimba
  • 26,717
  • 34
  • 141
  • 196
31
votes
3 answers

How expensive are dynamic casts in C++?

For my GUI API which works with a variety of backends (sdl, gl, d3d, etc) I want to dynamically cast the generic type image to whatever it may happen to be. So the bottom line is, I would be doing around 20 * 60fps dynamic casts per second. How…
jmasterx
  • 52,639
  • 96
  • 311
  • 557
30
votes
4 answers

dynamic_cast with RTTI disabled

I'm curious to know what happens when compiling code with a dynamic cast whith RTTI disabled (either with -fno-rttion GCC or with /GR- on visual studio). Does the compiler "falls back" to static_cast ? Since (at least on VS) it does only issue a…
Louen
  • 3,617
  • 1
  • 29
  • 49
29
votes
2 answers

What is qobject_cast?

Could someone explain in as simple terms as possible (or as simple as you would like) what qobject_cast is, what it does and why we would need to cast one class type to another? Like, I get typecasting in the sense of casting an int as a char or…
Michael Thomas
  • 443
  • 1
  • 4
  • 8
27
votes
3 answers

C++: "... is not a polymorphic type" while using boost::dynamic_pointer_cast

Why do I receive the following error for the following code? 1>C:\Libs\boost_1_44\boost/smart_ptr/shared_ptr.hpp(259): error C2683: 'dynamic_cast' : 'my_namespace::A' is not a polymorphic type 1> D:\[location]\[header_filename].h(35) : see…
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
27
votes
2 answers

When is dynamic_cast useful?

5.2.7/7 says something along the lines of: If T is "pointer to cv void", the result is a pointer to the most derived class pointed to by x. What is a good application of this syntax? When should dynamic_cast be used?
David G
  • 94,763
  • 41
  • 167
  • 253
1
2 3
41 42