Reputation: 279
Say there is a base class B and derived classes D1, D2, D3.
B* b;
D1 d1;
D2 d2;
D2 *pd2 = &d2;
D3 d3;
//more code placing valid data on variables
It is possible to say b = &d1
or b = pd2
somewhere in the code.
Now is it possible to compare b with pointers to d1, d2 and d3 for equality of pointers without dynamic casting? That is, can I do this:
if (b == &d1)
{
//do something
}
?
Upvotes: 4
Views: 1138
Reputation: 17339
Now is it possible to compare b with pointers to d1, d2 and d3 for equality of pointers without dynamic casting?
Yes.
if (b == pd2)
is perfectly fine to see if b
and pd2
point to the same object.
Upvotes: 2
Reputation: 21003
if (b == &d1)
will simply compare the pointer addresses unless an operator overload has been created that does a compare based on the information stored within the object.
As a side note, you cannot do b = pd2
based on your code, unless your overload the operator correctly within one or both objects, or instead use b = &pd2
Upvotes: 1