I have a class 'base' with a virtual destructor and thus a VTable and corresponding VTPR in it, and a class derived from it:
class base {
public:
virtual ~base() {}
};
class der : base {};
main()
{
int a = sizeof(base); // = 4 , fine !
int b = sizeof(der); // = 4 too ?
}
Now as derived class too is virtual, it'll have a VPTR of its own, but since it also has a subobject of the base class with a VPTR in it, shouldn't the size of the class 'der' be 8 bytes i.e. the size of the VPTR of the class 'der' + size of VPTR of the subobject of class 'base'? (when sizeof(void*) = 4 bytes ).
So basically my question is : When the subobject of class 'base' is made in 'der' does it have a seperate new VPTR ? And if it is so then why its size is not getting added while calculating the size of 'der'?
Can somebody please clarify this.
derinstance with abasepointer, how would the implementation know that it is supposed to fetch the vptr in the derived part of the object? However, it is interesting to note that an object can have multiple vptrs if it inherits from multiple polymorphic base classes:struct A { virtual ~A() {} }; struct B { virtual ~B() {} }; struct C : A, B {};. On some (most?) implementations, C will have two vptrs, one in theAsubobject and one in theBsubobject. - Luc Tourailledeleteand explicit dtor call. None of these are even allowed in a c-dtor: they aren't part of the virtual behavior of an object under construction (and they notably aren't part of the construction vtable for a virtual base for a base class ctor). If C was a triangle, not only its area would be zero, its diameter also! - curiousguydynamic_cast<T*>) and type identification (typeid); it's seems OK. Note: base class dtors need to set the vptr. - curiousguythisadjustment in a derived class overrider (which might be non zero in further derived) is the same. The offset from the most derived class is obviously the same and so are the offsets for casting to a base of that that derived type viadynamic_cast<T*>. - curiousguy