Please could you explain me why that marked lines don't compile?
The reason your code is not compiling is the fact that Foo and Bar are unrelated data types
From cplusplus.com
dynamic_cast can be used only with pointers and references to objects. Its purpose is to ensure that the result of the type conversion is a valid complete object of the requested class.
Therefore, dynamic_cast is always successful when we cast a class to one of its base classes
whereas
static_cast can perform conversions between pointers to related classes, not only from the derived class to its base, but also from a base class to its derived
and
const_cast manipulates the constness of an object, either to be set or to be removed
The reinterpret_cast, however, is the only choice you have for making things work:
reinterpret_cast converts any pointer type to any other pointer type, even of unrelated classes. The operation result is a simple binary copy of the value from one pointer to the other.
All pointer conversions are allowed: neither the content pointed nor the pointer type itself is checked.
There's no relationship between your two structs Foo and Bar, so the reinterpret_cast is the only choise available to you in this case.
If you take a look at the link I put above, you will see (in the reinterpret_cast section) the same identical example you provided.