fork download
  1. #include <iostream>
  2.  
  3. struct B
  4. {
  5. virtual ~B() {}
  6. };
  7.  
  8. struct D : B
  9. {
  10. };
  11.  
  12. int main() {
  13.  
  14. B* d = new D;
  15. B* b = new B;
  16.  
  17. D* p1 = (D*)b; // Сделает то же, что и reinterpret_cast. Т.е. в результате не nullptr
  18. std::cout << p1 << std::endl;
  19.  
  20. D* p2 = dynamic_cast<D*>(b); // Проверит, что b не указывает на экземпляр D и вернет nullptr
  21. std::cout << p2 << std::endl;
  22.  
  23. D* p3 = reinterpret_cast<D*>(b);
  24. std::cout << p3 << std::endl;
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0x8236018
0
0x8236018