Skip to content

Instantly share code, notes, and snippets.

@dodikk
Created October 3, 2018 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dodikk/2b49a6e7884c71cb7a32cabd299f77d9 to your computer and use it in GitHub Desktop.
Save dodikk/2b49a6e7884c71cb7a32cabd299f77d9 to your computer and use it in GitHub Desktop.
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