J
Jerry Coffin
[ ... ]
It sounds like you've already figured out that this statement was
backwards. It may have already been mentioned, but when casting up a
hierarchy, you don't normally need to use an explicit cast at all.
One is converting a pointer to a derived object to a pointer to an
inaccessible base. E.g.:
class base1 {};
class base2 {};
class derived : base1, base2 {};
int main() {
derived *d = new derived;
base1 *b1 = (base1 *)d;
base2 *b2 = (base2 *)d;
return 0;
}
There is no combination of static_cast, const_cast or dynamic_cast
that can give the results of C-style casts in this situation.
Since this uses private inheritance, neither an implicit conversion
nor a static_cast is allowed. A const_cast can only add or remove
cv-qualifiers, so it's a non-starter.
You could substitute reinterpret_cast for the C-style casts above, and
the compiler won't complain. The result will be incorrect though. If
you had only a single base class, there's a pretty fair chance that
things would work, even though IIRC you'd officially have undefined
behavior. When you have two (or more) base classes, however, a
reinterpret_cast _can't_ produce correct results. If you used
reinterpret_cast, the conversions to base1* and base2* would
necessarily produce pointers of those types, but with THE SAME
ADDRESS.
Since the two base classes canNOT have the same address, one of them
must be wrong. The C-style cast will actually produce two different
results (i.e. two different addresses), each containing the address of
the correct base subobject.
There are a number of variations on this theme, such as using
references instead of pointers to the objects, or using a
pointer/reference to a member instead of to the whole object.
Now (hopefully) you can see why I didn't really want to get into this:
it's a lot of explanation to cover something that virtually never
matters anyway.
Later,
Jerry.
For downcasts static_cast is better to be used. dynami_cast is for upcasting
and crosscasting.
It sounds like you've already figured out that this statement was
backwards. It may have already been mentioned, but when casting up a
hierarchy, you don't normally need to use an explicit cast at all.
Don't tease our curiosity. Tell it.
One is converting a pointer to a derived object to a pointer to an
inaccessible base. E.g.:
class base1 {};
class base2 {};
class derived : base1, base2 {};
int main() {
derived *d = new derived;
base1 *b1 = (base1 *)d;
base2 *b2 = (base2 *)d;
return 0;
}
There is no combination of static_cast, const_cast or dynamic_cast
that can give the results of C-style casts in this situation.
Since this uses private inheritance, neither an implicit conversion
nor a static_cast is allowed. A const_cast can only add or remove
cv-qualifiers, so it's a non-starter.
You could substitute reinterpret_cast for the C-style casts above, and
the compiler won't complain. The result will be incorrect though. If
you had only a single base class, there's a pretty fair chance that
things would work, even though IIRC you'd officially have undefined
behavior. When you have two (or more) base classes, however, a
reinterpret_cast _can't_ produce correct results. If you used
reinterpret_cast, the conversions to base1* and base2* would
necessarily produce pointers of those types, but with THE SAME
ADDRESS.
Since the two base classes canNOT have the same address, one of them
must be wrong. The C-style cast will actually produce two different
results (i.e. two different addresses), each containing the address of
the correct base subobject.
There are a number of variations on this theme, such as using
references instead of pointers to the objects, or using a
pointer/reference to a member instead of to the whole object.
Now (hopefully) you can see why I didn't really want to get into this:
it's a lot of explanation to cover something that virtually never
matters anyway.
Later,
Jerry.