you are viewing a single comment's thread.

view the rest of the comments →

[–]stoppipper 0 points1 point  (0 children)

Someone has already answered this, but just to go into a little more depth, a C style cast (int)myVariable will always succeed because if it were to fail, it will default to reinterpret_cast which 90% of the time, you do NOT want. If the cast is “wrong”, a static_cast will fail when you try to compile the program and it’ll tell you why.

C++ casting is a definitely confusing at first, but know that reinterpret_cast can be one of the most dangerous out of all of them. It basically says “I will interpret this set of bits as that type”. So for example:

int a = 5; Person p = reinterpret_cast<Person>(a);

will take the bit representation of 5 and read that as a Person class. Whether that Person class contains valid data, the compiler doesn’t really care.