Hi,
I decided to create a function that would do a dynamic cast to a pointer and throw and exception if the cast couldn't be done. Something like this:
1 2 3 4 5 6 7 8
void CastToParticle( Particle* P, Base* B) throw MyException {
try {
P = dynamic_cast<Particle*>( B );
} catch ( const std::bad_cast& e ) {
delete P;
throw MyException("Whatever");
}
}
The first version doesn't modify the P pointer in the caller, it modifies the P pointer in CastToParticle(). Pass either a Particle ** or a Particle *&.
A pointer is an integer, correct? Its assignment semantics are identical to an integer's, and this is sufficient for our example. If I change the type of p, my point becomes clear: