最初的代码其中y等于2f,最终结果显示为为1.45f-45
#include
using namespace std;
struct Base1 { int x; };
struct Base2 { float y; };
struct Derived :Base1, Base2{ };
int main()
{
Derived* pd = new Derived;
pd->x = 1; pd->y = 2.0f;
void* pv = pd;
Base2* pb = static_cast<Base2*>(pv);
cout << pd->x << " " << pb->y << endl;
delete pb;
return 0;
}
而如果隐示转换就没有问题。
#include
using namespace std;
struct Base1 { int x; };
struct Base2 { float y; };
struct Derived :Base1, Base2{ };
int main()
{
Derived* pd = new Derived;
pd->x = 1; pd->y = 2.0f;
void* pv = pd;
Base2* pb = pd;
cout << pd->x << " " << pb->y << endl;
delete pb;
return 0;
}
不知道为什么😳