Created
October 3, 2018 15:15
-
-
Save dodikk/2b49a6e7884c71cb7a32cabd299f77d9 to your computer and use it in GitHub Desktop.
Dynamic cast demo c++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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