First, in your example, there are no virtual base classes. Classes containing virtual functions are called polymorphic. (In C ++, there is such a thing as “virtual base classes,” but this has nothing to do with your example.)
Secondly, the behavior of your code does not depend on any virtual declarations. You intentionally destroyed the integrity of the base pointer using reinterpret_cast . For this reason, the behavior of the code is undefined.
Direct conversion from one base pointer to another (what you are trying to do in your code) is called cross-conversion. The only C ++ listing that can perform cross-listing is dynamic_cast .
t.push_back(dynamic_cast<T *>(u[0]));
You can do an indirect translation without dynamic_cast , but for this you need to first compress the pointer to the derived type ( A * ) using static_cast , and then convert it to another type of base pointer
t.push_back(static_cast<A *>(u[0]));
AnT
source share