Skip to content

Instantly share code, notes, and snippets.

@dodikk
Created Oct 3, 2018
Embed
What would you like to do?
Dynamic cast demo c++
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
class Parent
{
public:
virtual ~Parent() {}
};
class Child : public Parent
{
public:
virtual ~Child() {}
};
int main()
{
Parent* p = new Parent();
Child* c1 = static_cast<Child*>(p);
Child* c2 = dynamic_cast<Child*>(p);
::std::cout << "parent - " << p << "\n";
::std::cout << "static_cast - " << c1 << "\n";
::std::cout << "dynamic_cast - " << c2 << "\n";
return 0;
}
parent - 0x90cc20
static_cast - 0x90cc20
dynamic_cast - 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment