The standard states in 5.4.7 that C-style casts can actually do more than any sequence of new-style casts can do -- specifically including casting from a pointer-to-derived to pointer-to-base even when the base class is inaccessible, which is precisely what happens here with private inheritance. (Why this should be allowed, and in particular why it should be allowed only for C-style casts, is utterly beyond me; but it's undeniably allowed.)
So dribeas is right, compilers are obliged to handle the OP's C-style pointer conversion correctly, even when B
inherits from multiple base classes. My own testing with MSVC++8 and MinGW confirms his results in practice -- when B
inherits from multiple base classes, the compiler will adjust pointers when converting a B*
to an A*
or vice versa so that the correct object or subobject is identified.
I stand by my assertion that you ought to derive B
publicly from A
if you ever intend to treat a B
as an A
, since using private inheritance instead necessitates using C-style casts.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…