#include <iostream>
using namespace std;
//基类A
class A{
public:
A(int a);
public:
void display();
protected:
int m_a;
};
A::A(int a): m_a(a){ }
void A::display(){
cout<<"Class A: m_a="<<m_a<<endl;
cout<<"A:: &m_a=" <<&m_a<<endl;
}
//中间派生类B
class B: public A{
public:
B(int a, int b);
public:
void display();
protected:
int m_b;
};
B::B(int a, int b): A(a), m_b(b){ }
void B::display(){
cout<<"Class B: m_a="<<m_a<<", m_b="<<m_b<<endl;
cout<<"B:: &m_a=" <<&m_a<<" &m_b="<<&m_b<<endl;
}
//基类C
class C{
public:
C(int c);
public:
void display();
protected:
int m_c;
};
C::C(int c): m_c(c){ }
void C::display(){
cout<<"Class C: m_c="<<m_c<<endl;
cout<<"C:: &m_c=" <<&m_c<<endl;
}
//最终派生类D
class D: public B, public C{
public:
D(int a, int b, int c, int d);
public:
void display();
private:
int m_d;
};
D::D(int a, int b, int c, int d): B(a, b), C(c), m_d(d){ }
void D::display(){
cout<<"Class D: m_a="<<m_a<<", m_b="<<m_b<<", m_c="<<m_c<<", m_d="<<m_d<<"\n"<<endl;
cout<<"D:: &m_a=" <<&m_a<<" &m_b="<<&m_b<<" &m_c="<<&m_c<<" &m_d="<<&m_d<<endl;
}
int main(){
cout<<"--------初始化四个class----------"<<endl;
A *pa = new A(1);
cout<<"pa="<<pa<<endl;
pa ->display();
cout<<"-----------------------"<<endl;
B *pb = new B(2, 20);
cout<<"pb="<<pb<<endl;
pb ->display();
cout<<"-----------------------"<<endl;
C *pc = new C(3);
cout<<"pc="<<pc<<endl;
pc ->display();
cout<<"-----------------------"<<endl;
D *pd = new D(4, 40, 400, 4000);
cout<<"pd="<<pd<<endl;
pd ->display();
cout<<"-----------------------"<<endl;
cout<<"-----------赋值之后------------"<<endl;
pa = pd;
pa -> display();
cout<<"-----------------------"<<endl;
pb = pd;
pb -> display();
cout<<"-----------------------"<<endl;
pc = pd;
pc -> display();
cout<<"-----------------------"<<endl;
cout<<"pa="<<pa<<endl;
cout<<"pb="<<pb<<endl;
cout<<"pc="<<pc<<endl;
cout<<"pd="<<pd<<endl;
return 0;
}
/home/ledi/.CLion2016.2/system/cmake/generated/neww-a0ddff1d/a0ddff1d/Debug/neww
--------初始化四个class----------
pa=0x559deed7e280
Class A: m_a=1
A:: &m_a=0x559deed7e280
-----------------------
pb=0x559deed7e2a0
Class B: m_a=2, m_b=20
B:: &m_a=0x559deed7e2a0 &m_b=0x559deed7e2a4
-----------------------
pc=0x559deed7e2c0
Class C: m_c=3
C:: &m_c=0x559deed7e2c0
-----------------------
pd=0x559deed7e2e0
Class D: m_a=4, m_b=40, m_c=400, m_d=4000
D:: &m_a=0x559deed7e2e0 &m_b=0x559deed7e2e4 &m_c=0x559deed7e2e8 &m_d=0x559deed7e2ec
-----------------------
-----------赋值之后------------
Class A: m_a=4
A:: &m_a=0x559deed7e2e0
-----------------------
Class B: m_a=4, m_b=40
B:: &m_a=0x559deed7e2e0 &m_b=0x559deed7e2e4
-----------------------
Class C: m_c=400
C:: &m_c=0x559deed7e2e8
-----------------------
pa=0x559deed7e2e0
pb=0x559deed7e2e0
pc=0x559deed7e2e8
pd=0x559deed7e2e0
Process finished with exit code 0
在进行指针赋值之前,我们可以看出
在A 中 pa=&m_a
在B 中pb=&m_a (b继承了A)
在C 中pc=&m_c
在D中 pd=&m_a (D继承了B,C)
A | B | C | D |
a | a | a | |
b | b | ||
c | c | ||
d |
pa = pd;
pb = pd;
pc = pd;
之前有说过 将派生类对象赋值给基类对象时,会舍弃派生类新增的成员
将派生类的指针赋值给基类的指针的时候,比如 pa = pd;新增加的对象(bcd)在A中没有对应位置被丢弃,
pa 指向D 的第一个元素a 的地址 &m_a
A | D | ||
a | ---------------> | a | |
b | |||
c | |||
d |
pa 指向D 的第一个元素a 的地址 &m_a
B | D | ||
a | ---------------> | a | |
b | b | ||
c | |||
d |
pa 指向D 的第三个元素a 的地址 &m_c
C | D | ||
a | |||
b | |||
c | ---------------> | c | |
d |
参考1
参考2