CSDN博客

img wxcwuxuchun

C++对象模型之函数成员(3)

发表于2004/1/27 21:09:00  734人阅读

1.1.1. 多重继承下的虚成员函数

0001   class Base1
0002   {
0003   public :
0004     Base1();
0005   virtual ~Base1();
0006   virtual void speakClearly();
0007   virtual Base1 *clone() const;
0008   protected :
0009     float data_Base1;
0010   };
0011   class Base2
0012   {
0013   public :
0014     Base2();
0015     virtual ~Base2();
0016     virtual void mumble();
0017     virtual Base2 *clone() const;
0018   protected :
0019     float data_Base2;
0020   };
0021   class Derived : public Base1, public Base2
0022   {
0023   public :
0024     Dervied();
0025     virtual ~Dervied();
0026     virtual Dervied *clone() const;
0027   protected :
0028     float data_Dervied;
0029   };

Dervied支持虚函数(virtual function)的难度在于Base2 Subobject,有三个问题待解决:

1)   析构函数

2)   Base2::mumble();

3)   一组clone函数

多重继承下,一个派生类产生n-1个额外的虚函数表,用于表示基类(上一层)的数目,单一继承也可以看作特殊的多重继承。对象模型如下所示:

 

Base1 object             Base1 Virtual Function Table

data_Base1

_vptr_Base1

#0

type_info for Base1

#1

Base1::~Base1()

#2

Base1::clone()

#3

Bae1::SpeakClearly()


Base2 object             Base2 Virtual Function Table

data_Base2

_vptr_Base2

#0

type_info for Base2

#1

Base2::~Base2()

#2

Base2::mumble()

#3

Base2::clone()


 

 

 

#0

type_info for Derived

#1

Derived::~ Derived()

#2

Base1:: SpeakClearly()

#3

Derived::clone()

#4

Base2::mumble()

Derived object                    Derived Virtual Function Table(Shared with Base1)

Base1 Object

data_Base1

_vptr_Base1

Base2 Object

data_Base2

_vptr_Base2

data_Derived

 

 

 

 

 

 


                                              Base2_Derived Virtual Function Table

#0

type_info for Derived

#1

Derived::~ Derived()

#2

Base2::mumble()

#3

Derived::clone()

                                                  

 

 

 

 

 

当一个Derived对象指定给一个Base1或Derived指针,被处理的virtual Table为主要表格vtbl_Derived指针,而将一个Derived对象指派给一个Base2指针,被处理的virtual Table是次要表格vtbl_Base2_Derived。

有三种情况后继的Base Class会影响virtual function的支持:

1)   通过后继的Base Class指针调用Derived Class virtual function。也就是前述1)

Base2 *pb2 = new Derived;

delete pb2;

//C++伪代码

Derived *tmp = new Derived;

Base2 *pb2 = temp ? temp + sizeof(Base1) : 0;

(*pb2->vptr[1])(pb2 - sizeof(Base1));

2)   通过Dervied class 指针,调用后继的Base class中的继承而来的virtual function。也就是前述2)

Derived *pb = new Derived;

pd->mumble();

//C++伪代码

Derived *pd = new Derived;

(*pd->vptr[2])();

3)   发生在语言扩充性质下,允许一个virtual function 的返回值类型有所变化,可能是Base type也可能是publicly derived type。也就是前述3)

Base2 *pb1 = new Derived;

Base2 *pb2 = pb1->Clone();

//C++伪代码

Derived *tmp = new Derived;

Base2 *pb1 = temp ? temp + sizeof(Base1) : 0;

Derived *temp = (*pb1->vptr[3])();

Base2 *pb2 = temp ? temp + sizeof(Base1) : 0;
0 0

相关博文

我的热门文章

img
取 消
img