static_cast vs dynamic_cast

 



Static_cast 

#include <iostream>

int
main ()
{
  float a = 5.51;
  int b = static_cast < int >(a);
  std::cout << b;
}

Output

5

dynamic_cast

#include <iostream>

int
main ()
{
  float a = 5.51;
  int b = dynamic_cast < int >(a);
  std::cout << b;
}

Output

main.cpp:7:33: error: cannot dynamic_cast a (of type float) to type int (target is not pointer or reference)
    7 |   int b = dynamic_cast < int >(a);






Comments