example:
class A{
int x;
};
class B{};
class C : public A, public B {};
C c;
A* a = &c;
B* b = &c;
when I check the value of &c and b, they are different because b is after a in memory, but yet when I evaluate &c==b, they are the same, why is the case?
In the expression &c == b both operands have to be coerced to the same type. In this case &c (a C*) can be converted to B* as B is an accessible base class of C. This is exactly the same conversion as happens in B* b = &c so the resulting values are the same and the comparison returns true.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With