Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are pA,pB,pC not equal?

Consider the following program

 #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 are supposed to equal,but the result is

pA = 0031FD90

pB = 0031FD94

pC = 0031FD90

why pB = pA + 4? and when i change

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

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

to

class ClassA
{
};

class ClassB
{
};

the result is

pA = 0030FAA3

pB = 0030FAA4

pC = 0030FAA3

pB = pA + 1?

like image 211
denzel_wu Avatar asked Oct 17 '13 02:10

denzel_wu


1 Answers

The multiply inherited object has two merged sub-objects. I would guess the compiler is pointing one of the pointers to an internal object.

like image 116
Steve Wellens Avatar answered Sep 28 '22 06:09

Steve Wellens