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
8
votes
2 answers

what is side-cast or cross-cast in Dynamic_cast in C++

What is side-cast/cross-cast in Dynamic_cast in C++. Can someone explain with an example? #include using namespace std; class A { virtual void fn(){} }; class B:public A { }; class C:public A { }; int main() { B b; C*…
user1434287
  • 381
  • 3
  • 16
8
votes
1 answer

dynamic_cast vs static_cast to void*

In the last two lines of below program, static_cast and dynamic_cast behave differently. From what I understand, The result of a dynamic_cast always resolves to the address of the complete object. So it uses RTTI in some way.…
claudius
  • 1,112
  • 1
  • 10
  • 23
8
votes
3 answers

Is it safe to delete the pointer after dynamic_casting?

void foo(MyClass* myClass) { BaseClass* pBaseClass = dynamic_cast(myClass); delete myClass; // <-------------- Does this affects on pBaseClass ? } In general how dynamic_cast actually works? (does it work like a copy…
Dinushan
  • 2,067
  • 6
  • 30
  • 47
8
votes
6 answers

Should I change my design to prevent dynamic casts?

I have read several threads about dynamic casts in C++, all full of people claiming it indicates bad design. In other languages I never gave it much thought when checking the type of an object. I never use it as an alternative to polymorphism and…
Double Dan
  • 151
  • 4
8
votes
1 answer

Dynamic cast using Type of object C#

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…
Sharpac
  • 418
  • 3
  • 5
  • 13
7
votes
4 answers

Downcasting from base pointer to templated derived types

I have the following hierarchy: class base { public: virtual ~base(){} virtual void foo() {} }; template class derived1 : public base { virtual void foo() {}; }; template class derived2 : public base { virtual…
Xander Tulip
  • 1,438
  • 2
  • 17
  • 32
7
votes
5 answers

dynamic_cast of "this" inside constructor

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() {} …
cmannett85
  • 21,725
  • 8
  • 76
  • 119
7
votes
6 answers

dynamic_cast fails

I have a base class and a derived class. Each class has an .h file and a .cpp file. I am doing dynamic_cast of the base class object to the derived class in the following code: h files: class Base { public: Base(); virtual…
Igor
  • 26,650
  • 27
  • 89
  • 114
7
votes
1 answer

dynamic_cast issues: typeid object is not equal, but name is equal

I found that dynamic_cast didn't work in a situation where I expected it to, and looking at the typeid of the objects at runtime has made the situation even less clear. I just want a cast from base to derived, and I can't figure out why it's not…
Seth Johnson
  • 14,762
  • 6
  • 59
  • 85
7
votes
5 answers

How to use dynamic_cast to downcast correctly?

I am being very confused about dynamic_cast. Material from C++ Primer and cppreference(rule 5) can't help me understand. (cppreference is way much harder than the book and I read them both very carefully) From C++ Primer…
Rick
  • 7,007
  • 2
  • 49
  • 79
7
votes
1 answer

Dynamic_cast on non polymorphic types

I can understand why dynamic_cast does work in this case : #include struct A{ virtual ~A() = default; }; struct B { virtual ~B() = default; }; struct C : A, B{}; void f(const A &a) { if(auto p = dynamic_cast
Antoine Morrier
  • 3,930
  • 16
  • 37
7
votes
5 answers

dynamic cast with interfaces

I have a class with implements 2 interfaces and inherits 1 class. So, generally it looks like this: class T : public A, public IB, public IC { }; There is one point in the code where I have an IB *, but could really use an A *. I was hoping that a…
Evan Teran
  • 87,561
  • 32
  • 179
  • 238
7
votes
2 answers

What would be a use case for dynamic_cast of siblings?

I'm reading Scott Meyers' More Effective C++ now. Edifying! Item 2 mentions that dynamic_cast can be used not only for downcasts but also for sibling casts. Could please anyone provide a (reasonably) non-contrived example of its usage for siblings?…
sigil
  • 815
  • 8
  • 16
7
votes
4 answers

How is dynamic_cast typically implemented?

Is the type check a mere integer comparison? Or would it make sense to have a GetTypeId virtual function to distinguishing which would make it an integer comparison? (Just don't want things to be a string comparison on the class names) EDIT: What I…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
7
votes
9 answers

Is my method for avoiding dynamic_cast<> faster than dynamic_cast<> itself?

I was answering a question a few minutes ago and it raised to me another one: In one of my projects, I do some network message parsing. The messages are in the form of: [1 byte message type][2 bytes payload length][x bytes payload] The format and…
ereOn
  • 53,676
  • 39
  • 161
  • 238