fork download
  1. #include <iostream>
  2. #include <memory>
  3. using namespace std;
  4.  
  5. class Component {
  6. public:
  7. void show() { cout << "It's me, component "<<this<<endl; }
  8. virtual ~Component() {}
  9. };
  10.  
  11. class MeshComponent:public Component {
  12. public:
  13. void test() { cout << "It's me, mesh component "<<this<<endl; }
  14. };
  15.  
  16. const shared_ptr<Component> init() {
  17. return make_shared<MeshComponent>();
  18. }
  19.  
  20. int main() {
  21. auto p = init();
  22.  
  23. p->show();
  24. auto q = dynamic_pointer_cast<MeshComponent>(init());
  25. if (q) {
  26. q->show();
  27. q->test();
  28. }
  29. else cout << "Oh oh, something wrong happened"<<endl;
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 4316KB
stdin
Standard input is empty
stdout
It's me, component 0x561117bfce80
It's me, component 0x561117bfdeb0
It's me, mesh component 0x561117bfdeb0