Skip to main content

static_cast<>static_cast<> will only check if the types are compatible

In case 1 types are not directly compatible since the re is no operator to describe the copy relation between A and B

In case 2 the cast is a reference cast and as fastfar as the compiler is concerned A* can be casted to B* because they are compatible. The compiler will not know thatwhat the pointer a_ref holds so that's why it allows you to use it. dynamic_cast<>dynamic_cast<> checks the class the pointer points to also.

static_cast<> will only check if the types are compatible

In case 1 types are not directly compatible since the re is no operator to describe the copy relation between A and B

In case 2 the cast is a reference cast and as fast as the compiler is concerned A* can be casted to B* because they are compatible. The compiler will not know that the pointer a_ref holds so that's why it allows you to use it. dynamic_cast<> checks the class the pointer points to also.

static_cast<> will only check if the types are compatible

In case 1 types are not directly compatible since the re is no operator to describe the copy relation between A and B

In case 2 the cast is a reference cast and as far as the compiler is concerned A* can be casted to B* because they are compatible. The compiler will not know what the pointer a_ref holds so that's why it allows you to use it. dynamic_cast<> checks the class the pointer points to also.

Source Link
Raxvan
  • 6.5k
  • 2
  • 28
  • 47

static_cast<> will only check if the types are compatible

In case 1 types are not directly compatible since the re is no operator to describe the copy relation between A and B

In case 2 the cast is a reference cast and as fast as the compiler is concerned A* can be casted to B* because they are compatible. The compiler will not know that the pointer a_ref holds so that's why it allows you to use it. dynamic_cast<> checks the class the pointer points to also.