What does "pb may point to just B" mean exactly?
What does "pb may point to just B" mean exactly?
Why it is not safe to use static_cast?
Help needed.THANKS!!!
// static_cast_Operator.cpp
// compile with: /LD
class B {};
class D : public B {};
void f(B* pb, D* pd) {
D* pd2 = static_cast <D* >(pb); // not safe, pb may
// point to just B
B* pb2 = static_cast <B* >(pd); // safe conversion
}
[464 byte] By [
visame] at [2007-11-20 11:15:04]

# 1 Re: What does "pb may point to just B" mean exactly?
D is a type of B, but not all Bs are Ds.
Like all dogs are animals but not all animals are dogs.
If you have a pointer to a dog, you can cast it to a pointer to an animal because you know you are pointing to an animal.
But if you have a pointer to an animal you cannot cast it to a pointer to a dog, because it might not be a dog.