C++的多重继承和vtables
C++ multiple inheritance and vtables
因此,回到基础知识,我试图了解vtables之类的东西。在下面的示例中,如果我要将B*
传递给某个函数,那么该函数如何知道调用C
对象的vtable中的方法,而不是A
的vtable的方法?是否有两个独立的VTables被传递给该对象?接口指针真的只是vtables吗(因为接口IIRC不能包含属性声明)?
我想说的是,在我真正尝试这段代码之前,我一直认为你不能一次继承多个接口/类(可以说,所有接口都必须是线性的),这样vtable就可以自己构建了。
如果我对vtables如何工作的想法是正确的(我现在知道不是),那么传递一个B*
并调用B::OutB()
会调用A:OutA()
(显然不是这样)。
有人能照亮吗?
// Includes
#include <windows.h>
#include <iostream>
interface A
{
public:
virtual void OutA() = 0;
};
interface B
{
public:
virtual void OutB() = 0;
};
class C : public A, public B
{
public:
void OutA();
void OutB();
};
void C::OutA()
{
printf("Out An");
}
void C::OutB()
{
printf("Out Bn");
}
int main()
{
C obj;
obj.OutA();
obj.OutB();
A* ap = (A*)&obj;
B* bp = (B*)&obj;
ap->OutA();
bp->OutB();
system("pause");
// Return
return 0;
}
输出(如预期):
Out A
Out B
Out A
Out B
我不知道interface
是什么,因为:
interface
是而不是C++关键字- C++语义中没有"接口"的概念
- 不同的C++习惯用法或模式可以将单词interface用于不同的特定目的
- 其他语言使用"接口"来描述完全不同的实体(在Java中,它就像一种特殊的有限基类,在O’Caml中,它在C++中可能使用模板概念的地方使用)
但是,如果您正在编写C++,并且A
和B
是类,那么C
将包含两个子对象:A
和B
,并且每个子对象都有自己的vtable指针。
当把C++编译成C时,我们可以有:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
const int debug = 0;
void __pure_virtual_called() {
fputs ("pure virtual function calledn", stderr);
abort();
}
/* Translation of:
class A
{
public:
virtual void OutA() = 0;
};
*/
struct A;
typedef struct {
void (*ptr__OutA) (struct A *__this);
} vtable__A;
typedef struct A {
vtable__A *__vptr;
} A;
/* translation A::OutA()
* pure virtual function */
void A__OutA (A *__this) {
__pure_virtual_called();
}
vtable__A vtable__A__A = { .ptr__OutA = A__OutA };
void A__constructor (A *__this) {
if (debug)
printf ("A__constructor %pn", (void*)__this);
/* dynamic type is initialised to A */
__this->__vptr = &vtable__A__A;
}
/* Translation of:
class B
{
public:
virtual void OutB() = 0;
};
*/
struct B;
typedef struct {
void (*ptr__OutB)(struct B *__this);
} vtable__B;
typedef struct B {
vtable__B *__vptr;
} B;
/* translation B::OutB()
* pure virtual function */
void B__OutB (B *__this) {
__pure_virtual_called();
}
vtable__B vtable__B__B = { .ptr__OutB = B__OutB };
void B__constructor (B *__this) {
if (debug)
printf ("B__constructor %pn", (void*)__this);
/* dynamic type is initialised to B */
__this->__vptr = &vtable__B__B;
}
/* Translation of:
class C : public A, public B
{
public:
void OutA(); // overrides A::OutA()
void OutB(); // overrides B::OutB()
// note :
// no new virtual function
};
*/
/* no new virtual function
* so no specific vtable type! */
typedef struct {
/* no additional vptr, we already have 2! */
A base__A;
B base__B;
} C;
/******* upcasts
* translation of
* static_cast<C*> (p)
*/
/* translation of
* A *p;
* static_cast<C*> (p);
*/
C *static_cast__A__C (A *__ptr) {
/*
* base__A is first member of C
* so offsetof(C, base__A) == 0
* can skip the pointer adjustment
*/
return (C*)__ptr;
}
/* translation of
* B *p;
* static_cast<C*> (p);
*/
C *static_cast__B__C (B *__ptr) {
/* locate enclosing C object:
* __base__B is not first member
* need to adjust pointer
*/
return (C*)((char*)__ptr - offsetof(C, base__B));
}
/* translation of virtual functions of C
* overriding function declarations from A
*/
/* translation of C::OutA() */
/* C::OutA() called from C */
void C__OutA (C *__this) {
printf("Out A this=%pn", (void*)__this);
}
/* C::OutA() called from A */
void C__A__OutA (A *__this) {
if (debug)
printf ("C__A__OutA %pn", (void*)__this);
C__OutA (static_cast__A__C (__this));
}
vtable__A vtable__A__C = { .ptr__OutA = C__A__OutA };
/* translation of virtual functions of C
* overriding function declarations from B
*/
/* translation of C::OutB() */
/* C::OutB() called from C */
void C__OutB (C *__this) {
printf("Out B this=%pn", (void*)__this);
}
/* C::OutB() called from B */
void C__B__OutB (B *__this) {
if (debug)
printf ("C__B__OutB %pn", (void*)__this);
C__OutB (static_cast__B__C (__this));
}
vtable__B vtable__B__C = { .ptr__OutB = C__B__OutB };
void C__constructor (C *__this) {
if (debug)
printf ("C__constructor %pn", (void*)__this);
/* construct subobjects */
A__constructor (&__this->base__A);
B__constructor (&__this->base__B);
/* adjust dynamic type of this to C */
__this->base__A.__vptr = &vtable__A__C;
__this->base__B.__vptr = &vtable__B__C;
}
/* calls to C virtual functions with a C*
*/
/* translation of
* C *p;
* p->OutA();
*
* is
* ((A*)p)->OutA();
*
* because C::OutA() is overrides A::OutA()
*/
void dyn__C__OutA (C *__this) {
A *base_ptr__A = &__this->base__A;
base_ptr__A->__vptr->ptr__OutA (base_ptr__A);
}
/* translation of
int main()
{
C obj;
obj.OutA();
obj.OutB();
A *ap = &obj;
B *bp = &obj;
C *cp = &obj;
ap->OutA();
bp->OutB();
cp->OutA();
// Return
return 0;
}
*
*/
int main () {
/* translation of:
C obj;
*/
C obj;
C__constructor (&obj);
/* translation of:
obj.OutA();
obj.OutB();
* obj is a locally declared object
* so dynamic type of obj is known as C
* can make direct call to C::OutA(), C::OutB()
*/
C__OutA (&obj);
C__OutB (&obj);
/* dumb (zero optimisation) translation of:
A *ap = &obj;
B *bp = &obj;
C *cp = &obj;
*/
A *ap = &obj.base__A;
B *bp = &obj.base__B;
C *cp = &obj;
/* translation of:
ap->OutA();
bp->OutB();
cp->OutA();
* dumb compiler = no optimisation
* so dynamic type of *ap, *bp, *cp is unknown
* so make "virtual" calls using vtable
*/
ap->__vptr->ptr__OutA(ap);
bp->__vptr->ptr__OutB(bp);
dyn__C__OutA (cp);
/* note: obj lifetime ends now
* C has a trivial destructor
* so no destructor call needed
*/
return 0;
}
请参阅http://ideone.com/TioyX
输出:
Out A this=0xbfeee2ec
Out B this=0xbfeee2ec
Out A this=0xbfeee2ec
Out B this=0xbfeee2ec
Out A this=0xbfeee2ec
通过多重继承,对象分部分构建,每个部分对应一个基类。这包括vtable指针。这是必要的,因为与指针或引用交互的代码不知道它是与基类还是派生类一起工作,所以它们的布局必须相同。
一个令人惊讶的结果是,当您将指针投射到某个基类时,它的地址可能会更改!编译器在后台生成一些代码,将指针调整到对象的适当部分。
C obj;
A* ap = (A*)&obj;
B* bp = (B*)&obj;
bool same = ((void*)ap) == ((void*)bp); // false!
相关文章:
- 关于C++中具有多重继承"this"指针的说明
- C++中模板化异常类的多重继承
- 虚拟继承中是否存在多重继承?
- 如何在 c++ 多重继承中调用父非虚函数?
- 多重继承相同的方法名,没有歧义
- 使用enable_if解决多重继承歧义
- 多重继承导致虚假的模糊虚拟函数过载
- 多重继承和访问不明确的元素
- C++ 多重继承:使用基类 A 的实现实现基类 B 的抽象方法
- 多重继承中的派生类的行为类似于聚合
- 为什么我的 Hippomock 期望在使用多重继承时失败
- 带有此指针的模板类多重继承构造函数不起作用?
- 使用多重继承时出现编译错误
- 增强多重继承的序列化
- 多重继承:跳过'virtual'关键字并拒绝菱形层次结构的使用?
- 仅函数的多重继承 - 没有虚拟和 CRTP
- C++多重继承和鸭子类型
- C++两次从文件保存对象读取多重继承
- 从多重继承中的派生类函数调用适当的父类函数
- C++的多重继承和vtables