fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4.  
  5. template<typename T>
  6. class A
  7. {
  8. public: T x;
  9. virtual void v(){};
  10. };
  11.  
  12.  
  13. int main() {
  14.  
  15. A<int> a1;
  16. A<float> a2;
  17. cout<<typeid(a1).name()<<endl;
  18. cout<<typeid(a2).name()<<endl;
  19. void * pv = static_cast<void*>(&a2);
  20. A<int> * pai = static_cast<A<int>*>(pv);
  21.  
  22. cout<<typeid(pai).name()<<endl; //pointer to A<int>
  23. cout<<typeid(*pai).name()<<endl; //A<float>!
  24. A<float> *dynamic = dynamic_cast<A<float>*>(pai); //funny, donno what happens here
  25. if(dynamic != nullptr) {
  26. cout<<typeid(*dynamic).name()<<endl;
  27. } else {
  28. cout<<"dynamic_cast failed!"<<endl;
  29. }
  30. return 0;
  31. }
Success #stdin #stdout 0s 4556KB
stdin
Standard input is empty
stdout
1AIiE
1AIfE
P1AIiE
1AIfE
dynamic_cast failed!