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

Is it valid to cast from A* to B* via dynamic_cast when A and B haven't a common ancestor?

Both clang 3.5.0 and g++ 4.9.0 compile the following code fine (with -std=c++11 -Wall -Wextra -pedantic-errors) and the program outputs true: #include struct A { virtual ~A() = default; }; struct B { virtual ~B() =…
Constructor
  • 7,273
  • 2
  • 24
  • 66
7
votes
5 answers

Accessing subclass members from a baseclass pointer C++

I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these. I have a function to go through the array, determine the subtype of each…
David Mason
  • 2,917
  • 4
  • 30
  • 45
7
votes
2 answers

How to use dynamic_cast with for_each

I have the following code: vector::iterator itr = vec.begin(); for (; itr != vec.end(); ++itr) { C2 *c = dynamic_cast(*itr); c->f(); } I am wondering if I could use one-line for_each to replace it. I tried the…
my_question
  • 3,075
  • 2
  • 27
  • 44
7
votes
2 answers

In c++11, does dynamic_cast return nullptr or 0?

I want to check the result of dynamic_cast. In c++11 (or c++0x, for compilers that support nullptr), should I compare against nullptr or 0? Does it matter, and if so, why? Is the result compiler-dependent?
Patrick
  • 2,243
  • 2
  • 23
  • 32
7
votes
3 answers

C++: dynamic_cast causes a SEGFAULT even when the object that is casted is not NULL. How can that happen?

Suppose I have a class A and a class B that is derived from A. Now, I want to cast a const A* (called "a") to a B* using dynamic_cast (see below). If "a" really was a B*, then my resulting object pointer should be fine. If "a" was not a B*, then I…
pvh1987
  • 621
  • 5
  • 8
  • 12
6
votes
2 answers

Variadic templates and dynamic cast

I have a piece of C++ code as follows: template struct CastAll{ template void cast_all(void(*fun)(B...), A...as){ //... } }; What I'd like to do is to implement cast_all in such a way that it dynamic-casts…
6
votes
1 answer

Losing RTTI info after returning from a function

Given a class and subclass: class Event {...} class Note : public Event {...} A Note is Cloned and stored in a pointer within a function f(). The type-information is preserved in the pointer and can be recovered by dynamic_cast: void f() { …
Christof Schardt
  • 551
  • 1
  • 5
  • 16
6
votes
4 answers

How to properly use "C++ Core Guidelines: C.146: Use dynamic_cast where class hierarchy navigation is unavoidable"

Motivation The C++ Core Guidelines recommends using dynamic_cast when "class hierarchy navigation is unavoidable." This triggers clang-tidy to throw the following error: Do not use static_cast to downcast from a base to a derived class; use…
6
votes
7 answers

C#: Casting types dynamically

I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar Arg2) { if (MainObject is SomeClassType1) { SomeClassType1 HelpObject = (SomeClassType1)MainObject; HelpObject.Property1 = Arg1; …
MK_Dev
  • 3,291
  • 5
  • 27
  • 45
6
votes
4 answers

How do I dynamically cast from a void * pointer generically?

class BASE { public: virtual ~BASE() {} void lamp() { cout << "\nBASE CLASS"; } }; class DERIVED : public BASE { public: void fun(); }; void DERIVED::fun() { cout << "\nDERIVED CLASS!"; } int main() { BASE * pbase…
6
votes
1 answer

Why dynamic_cast is ok to use for upcast for non polymorphic types?

See here: dynamic_cast can only be used with pointers and references to classes (or with void*). Its purpose is to ensure that the result of the type conversion points to a valid complete object of the destination pointer type. This naturally…
Narek
  • 38,779
  • 79
  • 233
  • 389
6
votes
4 answers

Looking for a pattern to avoid using dynamic_cast in implementation of a virtual function

My problem is this: I have an interface root class with several concrete branch classes. In application code, there exists a vector of pointers to the root class. There are several places where I need to loop over all the elements in the vector and…
6
votes
2 answers

Downcasting using dynamic_cast returns null

I'm trying to cast a base class object to a derived class object with dynamic_cast, but dynamic_cast returns null. Is it possible to downcast using dynamic_cast? struct A { virtual ~A() {} }; struct B : A {}; int main() { A* a = new A(); …
user3828398
  • 495
  • 3
  • 9
  • 18
6
votes
2 answers

The reason why dynamic_cast doesn't work with non-polymorphic types

With classes B and derived class D: class B { int b; }; class D : public B { int d; }; D* d = new D(); B* b = dynamic_cast(d); the above will work fine—it's a simple upcasting. We're sure that whatever b is pointing at has the B…
user5539357
  • 1,024
  • 1
  • 9
  • 19
6
votes
2 answers

dynamic cast a reference and auto

I've encountered a pretty weird behavior when using auto and dynamic_cast. This is the class hierachy i have: class BaseInterface { public: virtual void someMethod()=0; }; class Derived:public BaseInterface { public: virtual void…
user1965150