为什么下面的代码打印01
?
我预计00
。如果地址不相等,为什么 operator== 在第一种情况下返回 true?
#include <iostream>
class B1
{
int m_i;
};
class B2
{
double m_d;
};
class D
: public B1
, public B2
{
};
int main()
{
D d;
B2 *b2 = &d;
std::cout << "d:\t" << reinterpret_cast<void*>(&d) << "\t" << &d << "\n";
std::cout << "b2:\t" << reinterpret_cast<void*>(b2) << "\t" << b2 << "\n";
std::cout << (reinterpret_cast<void*>(b2) == reinterpret_cast<void*>(&d));
std::cout << (b2 == &d);
return 0;
}
给出输出:
d: 0xbfd65fa4 0xbfd65fa4
b2: 0xbfd65fa8 0xbfd65fa8
01