其实也很简单,因为首地址相同,通过指针访问就可以了。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class A {
int a = 10;
};
class B:A {
int b;
public:
B() {
*(int *)this = 20;
cout << *(int *)this << endl;
}
};
int main(int argc, char* argv[])
{
cout << sizeof(B) << endl;
B b;
system("pause");
return 0;
}
本文探讨了C++中多重继承的实现细节,通过一个具体示例展示了基类与派生类之间的内存布局,解释了如何通过指针访问基类成员,并分析了sizeof运算符在多重继承场景下的表现。
781

被折叠的 条评论
为什么被折叠?



