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
12
votes
4 answers

error: cannot dynamic_cast ... (target is not pointer or reference)

I'm learning exception handling in C++ and run into a problem. Here's the code: #include #include using namespace std; class A { public: virtual void f(void){} }; class AA:public A { public: void aa(void){}; }; int…
focusHard
  • 271
  • 1
  • 5
  • 12
11
votes
2 answers

dynamic_cast in assert Causing Error

I'm using the antiquated Visual Studio 2008 (let me save you the trouble "there's your problem".) This seems to be a problem with Visual Studio: http://rextester.com/XKFR77690 This seems to be a problem with the assert macro:…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
11
votes
2 answers

When dynamic_cast will throw exception in case used with pointer?

I am using dynamic_cast in my source to cast pointer as some thing like below, Base *base = here storing the pointer; Derived *derived = dynamic_cast(base); In the case of base doesn't have the pointer of the class hierarchy then cast…
srajeshnkl
  • 883
  • 3
  • 16
  • 49
10
votes
2 answers

How fast is dynamic_cast<>

... approximately compared to a typical std::string::operator==()? I give some more details below, I'm not sure if they are of any relevance. Answer with complexity or approximation is good enough. Thanks! Details: I will use it inside a for loop…
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
10
votes
2 answers

Explicit instantiation of a templated class and dynamic_cast in a shared library

I stumbled into a problem today that I can't seem to solve. I am compiling a shared library that includes a templated class (Derived, whose base is Base) and some explicit instantiations of this class. I would like the library user to extend from…
YuppieNetworking
  • 8,672
  • 7
  • 44
  • 65
10
votes
3 answers

dynamic_cast confusion

I give up on this... $5.2.7/2- "If T is a pointer type, v shall be an rvalue of a pointer to complete class type, and the result is an rvalue of type T. If T is a reference type, v shall be an lvalue of a complete class type, and the …
Chubsdad
  • 24,777
  • 4
  • 73
  • 129
10
votes
4 answers

Complex dynamic_cast in c++

I have the following case in C++: Abstract base classes Abstract1 and Abstract2. They are unrelated. A class Foo deriving from both Abstract1 and Abstract2 I am in a compilation unit where I have no information about class Foo (no declaration, no…
galinette
  • 8,896
  • 2
  • 36
  • 87
10
votes
3 answers

Why is dynamic_cast evil or not ? Should I use dynamic_cast in this case?

Some say the use of dynamic_cast often means bad design and dynamic_cast can be replaced by virtual functions why is the use of dynamic_cast considered bad design? Suppose I have I function name func(Animal* animal, int animalType) , the…
Gary Gauh
  • 4,984
  • 5
  • 30
  • 43
9
votes
5 answers

Are dynamic_casts safe to remove in production code?

dynamic_casts are slower, but they are safer than static_casts (when used with object hierarchies, of course). My question is, after I've ensured in my debug code that all (dynamic) casts are correct, is there any reason for me not to change them to…
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
9
votes
2 answers

Is the compiler allowed to optimize out dynamic_cast of a volatile pointer when the compiler doesn't see a possible type which can fulfill the cast?

Look at this little snippet: struct A { virtual ~A() { } }; struct B { }; bool fn() { A *volatile a = new A; return dynamic_cast(a); } Is the compiler allowed to remove the dynamic_cast altogether, and transform dynamic_cast to…
geza
  • 28,403
  • 6
  • 61
  • 135
9
votes
2 answers

How do I dynamic upcast and downcast with smart pointers?

I have some code with a generic interface in which I need to cast up and down. I'm trying to convert to smart pointers now, but am running into some errors. The code below reproduces the problem. I'm using C++14, so I think this stuff is supposed to…
Carbon
  • 3,828
  • 3
  • 24
  • 51
9
votes
1 answer

How is the deletion of a pointer detected using dynamic cast

As shown here, one can use dynamic_cast to detect a deleted pointer: #include using namespace std; class A { public: A() {} virtual ~A() {} }; class B : public A { public: B() {} }; int main() { B* pB = new B; cout <<…
azerty
  • 698
  • 7
  • 28
8
votes
1 answer

Fast dynamic casting progress

A little while ago, I found that very interesting paper on a very neat performance upgrade for dynamic_cast in C++: http://www2.research.att.com/~bs/fast_dynamic_casting.pdf. Basically, it makes dynamic_cast in C++ way faster than the traditional…
8
votes
3 answers

Dynamic cast in destructor

Is this code legal? class Base1 { }; class Base2 { public: virtual ~Base2() { if (!dynamic_cast(this)) std::cout << "aaaa" << std::endl; } Base2() { } }; class MyClass: public Base1, public Base2…
greywolf82
  • 21,813
  • 18
  • 54
  • 108
8
votes
3 answers

Checking whether a cross-cast could possibly work?

I know that it's legal to use dynamic_cast to do a "cross-cast" across a class hierarchy. For example, if I have classes that look like this: A B \ / C If I have an A* pointer that's pointing at an object of type C, then I can use A*…
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065