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
25
votes
3 answers

FAQ: Why does dynamic_cast only work if a class has at least 1 virtual method?

This does not compile in C++: class A { }; class B : public A { }; ... A *a = new B(); B *b = dynamic_cast(a);
Filip Frącz
  • 5,881
  • 11
  • 45
  • 67
25
votes
3 answers

How to identify failed casts using dynamic_cast operator?

Scott Meyer in his book Effective C++ says dynamic_cast is used to perform safe casts down or across an inheritance hierarchy. That is, you use dynamic_cast to cast pointers or references to base class objects into pointers or references to derived…
nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
24
votes
5 answers

c++ dynamic_cast error handling

Is there any good practice related to dynamic_cast error handling (except not using it when you don't have to)? I'm wondering how should I go about NULL and bad_cast it can throw. Should I check for both? And if I catch bad_cast or detect NULL I…
Nazgob
  • 8,502
  • 4
  • 40
  • 42
23
votes
6 answers

What are some 'good use' examples of dynamic casting?

We often hear/read that one should avoid dynamic casting. I was wondering what would be 'good use' examples of it, according to you? Edit: Yes, I'm aware of that other thread: it is indeed when reading one of the first answers there that I asked my…
OysterD
  • 6,660
  • 5
  • 34
  • 33
21
votes
4 answers

How does dynamic_cast work?

If you had the following: class Animal{}; class Bird : public Animal{}; class Dog : public Animal{}; class Penguin : public Bird{}; class Poodle : public Dog{}; Does dynamic_cast just check if one class is a derived class of another, or if one…
Bob John
  • 3,688
  • 14
  • 43
  • 57
20
votes
1 answer

How does dynamic_cast fail?

According to what I read, performing a wrong run-time dynamic_cast can either throw a bad_cast exception or return zero. Is it correct to say that it will return zero if you are casting pointers? i.e: class Base { virtual void a(){} }; class…
NIGO
  • 863
  • 2
  • 10
  • 16
20
votes
5 answers

What is the proper use case for dynamic_cast?

I have been told many times (and seen myself in practice) that the use of dynamic_cast often means bad design, because it can and should be replaced with virtual functions. For example, consider the following code: class Base{...}; class…
user500944
19
votes
4 answers

What are the arguments to the types.CodeType() python call?

I'm currently trying to roll my own "marshal" code for python so i can store compiled python code on Google App Engine to serve scripts on a dynamic way. As you all can verify, "marshal" isn't supported on GAE and "pickle" can't serialize code…
19
votes
6 answers

What could cause a dynamic_cast to crash?

I have a piece of code looking like this : TAxis *axis = 0; if (dynamic_cast(obj)) axis = (dynamic_cast(obj))->GetXaxis(); Sometimes it crashes : Thread 1 (Thread -1208658240 (LWP 11400)): #0 0x0019e7a2…
Barth
  • 15,135
  • 20
  • 70
  • 105
18
votes
10 answers

How can I avoid dynamic_cast in my C++ code?

Let's say I have the following class structure: class Car; class FooCar : public Car; class BarCar : public Car; class Engine; class FooEngine : public Engine; class BarEngine : public Engine; Let's also give a Car a handle to its Engine. A…
Michael Kristofik
  • 34,290
  • 15
  • 75
  • 125
16
votes
2 answers

dynamic_cast and rvalue reference

class A{ public: virtual ~A() {}; }; class B : public A{ }; int main(){ A&& p = B(); dynamic_cast(std::move(p)); } Throws the error (g++ 5.2.0): error: conversion to non-const reference type 'std::remove_reference::type&…
SergeantPenguin
  • 843
  • 7
  • 16
16
votes
3 answers

Can a standard-compliant compiler reject code containing dynamic_cast downcast from non-polymorphic type?

This question is inspired by comments here. Consider the following code snippet: struct X {}; // no virtual members struct Y : X {}; // may or may not have virtual members, doesn't matter Y* func(X* x) { return dynamic_cast(x); } Several…
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
15
votes
3 answers

how to check whether two pointers point to the same object or not?

Consider two pointers A* a; B* b; Both A and B are polymorphic classes. How to check whether a and b point to the same object or not? More precisely, let's specify a and b point to the same object if there exists some object d of type D such that…
user396672
  • 3,106
  • 1
  • 21
  • 31
15
votes
6 answers

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

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…
user34537
15
votes
4 answers

boost::shared_ptr and dynamic cast

I have a problem using a shared_ptr of a base class, I can't seem to be able to call the derived class's methods when dereferencing it. I believe code will be more verbose than me: class Base : public boost::enable_shared_from_this { …
HolyLa
  • 153
  • 1
  • 1
  • 4
1
2
3
41 42