gpt4 book ai didi

c++ - 为什么 pA、pB、pC 不相等?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:46:00 24 4
gpt4 key购买 nike

考虑下面的程序

 #include<iostream>
using namespace std;
class ClassA
{
public:
virtual ~ClassA(){};
virtual void FunctionA(){};

};

class ClassB
{
public:
virtual void FunctionB(){};
};



class ClassC : public ClassA,public ClassB
{

};

void main()
{
ClassC aObject;
ClassA* pA = &aObject;
ClassB* pB = &aObject;
ClassC* pC = &aObject;

cout<<"pA = "<<pA<<endl;
cout<<"pB = "<<pB<<endl;
cout<<"pC = "<<pC<<endl;

}

pA,pB,pC应该是相等的,结果却是

pA = 0031FD90

pB = 0031FD94

pC = 0031FD90

为什么 pB = pA + 4?当我改变时

class ClassA
{
public:
virtual ~ClassA(){};
virtual void FunctionA(){};

};
class ClassB
{
public:
virtual void FunctionB(){};
};

class ClassA
{
};

class ClassB
{
};

结果是

pA = 0030FAA3

pB = 0030FAA4

pC = 0030FAA3

pB = pA + 1?

最佳答案

多重继承的对象有两个合并的子对象。我会猜测编译器将其中一个指针指向内部对象。

关于c++ - 为什么 pA、pB、pC 不相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19417340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com