l*******y 发帖数: 1498 | 1 If you inherit a base class as virtual, only one subobject of that class
will ever appear as a base class. 下面这个例子:
#include
#include
using namespace std;
class MBase {
public:
int i_base;
virtual char* vf() const = 0;
virtual ~MBase() {}
};
class D1 : virtual public MBase {
public:
char* vf() const { return "D1"; }
};
class D2 : virtual public MBase {
public:
char* vf() const { return "D2"; }
};
// MUST explicitly disambiguate vf():
class MI : public D1, public D2 { |
s**x 发帖数: 7506 | |
l*******y 发帖数: 1498 | 3 是我搞错了。。。以为所有的object都共享一个copy |
l*****a 发帖数: 14598 | 4 每个类有自己的Vtable,然后constructor会生成自己的Vptr指向自己的Vtable。
【在 l*******y 的大作中提到】 : If you inherit a base class as virtual, only one subobject of that class : will ever appear as a base class. 下面这个例子: : #include : #include : using namespace std; : class MBase { : public: : int i_base; : virtual char* vf() const = 0; : virtual ~MBase() {}
|
s**x 发帖数: 7506 | 5 你好像把 downcast/upcast 搞反了。 |