This behavior is undefined. Whether this "works" or not on any compiler, perhaps beyond this; you cannot rely on this job at all. *
In the scenario you described, it seems like the best solution is to simply create some free functions that take Base as an argument. Of course, if the required functionality depends on the members of protected , then you have a problem! (And I'm not sure there is a good solution, except to find a way to avoid the need for such access.)
* And it's true even if you never change your compiler. The compiler may assume that all of the code is “correct,” so a small change in the code may cause optimizations that make this trick useless.(Of course, I'm not going to assume that this will happen definitely , just so that I can ). Sub>
source share