I wonder how pointers are compared when it comes to multiple inheritance. It seems that in some circumstance, they are equal although the address is obviously different. See the following code:
class A {
int m_i;
};
class B {
double m_d;
};
class C: public A, public B {
char m_c;
};
int main() {
C c;
A *pa = &c;
B *pb = &c;
std::cout << "Address of c is: " << &c << std::endl;
std::cout << "A type pointer to c is: " << pa << std::endl;
std::cout << "B type pointer to c is: " << pb << std::endl;
std::cout << "if pa == &c: " << (pa == &c) << std::endl;
std::cout << "if pb == &c: " << (pb == &c) << std::endl;
return 0;
}
The output is:
Address of c is: 00F6F870
A type pointer to c is: 00F6F870
B type pointer to c is: 00F6F878
if pa == &c: 1
if pb == &c: 1
There is no wonder pb is different from &c. The weird thing is (pb == &c) delivers 1. I am now confused how (pb == &c) is actually evaluated.
B *pb = &c;produces an address different from&c? In both cases,&cis converted toB*- first time for the initialization, the second time for the comparison. Is it really a wonder that the same process repeated twice produces the same outcome?2 == 2.0compares to true? They have to be converted to the same type to do the comparison, right?