Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) dynamic_cast

 

dynamic_cast is a keyword to cast data types in the same inheritance hierarchy, for example an Animal to a Monkey.

 

 

There are two types of dynamic_cast:

  1. From a derived class to a base class (for example, from a Monkey to an Animal): this is called an upcast and will always succeed (as every Monkey is an Animal)
  2. From a base class to a derived class (for example, from an Animal to a Monkey): this is called a downcast and will not always succeed (as not every Animal is a Monkey). If a downcast cannot succeed, dynamic_cast returns an empty pointer

 

 

It is not possible to use dynamic_cast on smart pointers, use boost::dynamic_pointer_cast instead.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppDynamic_cast.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt

SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#include <cassert>
#include <boost/shared_ptr.hpp>

struct Base
{
  //All base classes must have a (public) virtual destructor.
  virtual ~Base() {}
};

struct Derived : public Base { };

int main()
{
  //Base initialized with derived, can be downcasted to derived
  {
    Base * const p1 = new Derived;
    Derived * const p2 = dynamic_cast<Derived*>(p1);
    assert(p1);
    assert(p2);
    assert(p1 == p2);
    delete p1;
    //delete p2; //Do not re-delete!
  }
  //Base initialized with base, cannot be downcasted to derived
  {
    Base * const p1 = new Base;
    Derived * const p2 = dynamic_cast<Derived*>(p1);
    assert( p1);
    assert(!p2);
    assert(p1 != p2);
    delete p1;
  }
  //Const base initialized with derived, can be downcasted to const derived
  {
    Base * const p1 = new Derived;
    const Derived * const p2 = dynamic_cast<const Derived*>(p1);
    assert(p1);
    assert(p2);
    assert(p1 == p2);
    delete p1;
    //delete p2; //Do not re-delete!
  }
  //Const base initialized with base, cannot be downcasted to const derived
  {
    const Base * const p1 = new Base;
    const Derived * const p2 = dynamic_cast<const Derived*>(p1);
    assert( p1);
    assert(!p2);
    assert(p1 != p2);
    delete p1;
  }
  //Const base initialized with derived, cannot be down- and const-casted to non-const derived
  {
    const Base * const p1 = new Derived;
    //Removing const: will and should not compile
    //Derived * const p2 = dynamic_cast<Derived*>(p1));
    assert(p1);
    delete p1;
  }
  //Base initialized with derived, can be downcasted to const derived with adding constness
  {
    const Base * const p1 = new Derived;
    //Adding const: will and should compile
    const Derived * const p2 = dynamic_cast<const Derived*>(p1);
    assert(p1);
    assert(p2);
    assert(p1 == p2);
    delete p1;
    //delete p2; //Do not re-delete!
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml