Virtual Pointer for Virtual table

Is virtual pointer inherits to derived class?

I tried this and got the ans that 'yes'.

Pleas check following example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class ClassABC
{
public:
    ClassABC(){}
    virtual void Display(){ cout<<"\nClassABC::Display()\n"; }
private:
    int _a;
};

class ClassXYZ:public ClassABC
{
public:
    ClassXYZ(){}
    virtual void Display(){ cout<<"\nClassXYZ::Display()\n"; }
private:
    int _b;
};


int main()
{
    cout<<"\n Test virtual ptr inherits to derived class \n";
    ClassABC obj1;
    cout<<"\n Size of ClassABC::obj1 : "<<sizeof(obj1);

    ClassXYZ obj2;
    cout<<"\n Size of ClassXYZ::obj2 : "<<sizeof(obj2);
}

Output=========================
Size of ClassABC::obj1 : 8
Size of ClassXYZ::obj2 : 12
===============================

But ClassXYZ also have virtual function "Display2()" so it should have its own Virtual pointer,So size of obj2 should be:
from base class : 8
_b : 4
ClassXYZ::vptr : 4
So total is 16 bytes.

Please explain me why is 12 bytes?
Last edited on
Why it would de 16?

Base class:
Own data: 4 bytes
Pointer to vtable: 4 bytes (on your machine)
Total: 8 bytes

Derived class:
Inherited data from base class: 4 bytes.
Own data: 4 bytes
Pointer to vtable: 4 bytes (on your machine)
Total: 12 bytes

On modern 64bit machine it would be 12 and 16 bytes respectively as size of pointer is 8 byte here
Thanks MiiNiPaa to reply.

According to you if we remove below virtual function from Derived class
 
virtual void Display(){ cout<<"\nClassXYZ::Display()\n"; }


we get the size of derived class object is 8 bytes?

Because,
Inherited data from base class: 4 bytes.
Own data: 4 bytes
And no Virtual function so no Vptr to virtual table needed right.

But thing is that Actually size become in above case is 12 bytes only. Because 4 bytes(Vptr) from Base class is inherits into Derived class.


And no Virtual function
There is one inherited from Base, so vtable pointer is still needed. If your class has virtual functions (declared in it or inherited from parent) add vtable cost.

Another thing which can change expected class size is padding: unused bytes added to ensure aligment.
For example I would expect following structure to have size of 16 bytes
1
2
3
4
5
struct foo 
{
    int i;
    double d;
};
Last edited on
Thanks to reply.

My simple question is one Vptr required for a virtual table in each class.

Now, one Vptr is inherited from Base class and another required for own class.
So total two Vptr required right?

If yes then size of object of Derived class i.e. ClassXYZ should be 16 bytes.

If No that means only one Vptr is there in Derived class, its from inherited one?

I am confused....please reply :)
Now, one Vptr is inherited from Base class and another required for own class.
No. It is not inherited, as it does not exist as far as language rules concerned. You cannot inherit something which does not exist. Virtual table is implementation detail, added by compiler to make virtual magic work. You either have vtable or not.

Bonus: You can have more than one vtable in case of multiple inheritance.
https://en.wikipedia.org/wiki/Virtual_method_table

Additionally: compiler does not required to use vtable for dynamic dispatch, it just most efficient way and every compiler you see uses this approach.
Ho is it...?

That means Vptr of base class not inherits to its Derived class?

Ok then its correct :

ClassABC : object size - 8 bytes (4 for int member and +4 for Vptr)
ClassXYZ : object size - 12 bytes(4 from Base class +4 for int member and +4 for OwnVptr)


Yes. As all of those members has same aligment, no padding bytes. So size is size of all members + vtable pointer.
Hi,

As you said virtual pointer to virtual table not inherits from base class to derived class then why below code gives such output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class ClassABC
{
public:
    ClassABC(){}
    virtual void Display(){ cout<<"\nClassABC::Display()\n"; }
private:
    int _a;
};

class ClassXYZ:public ClassABC
{
public:
    ClassXYZ(){}
private:
    int _b;
};


int main()
{
    cout<<"\n Test virtual ptr inherits to derived class \n";
    ClassABC obj1;
    cout<<"\n Size of ClassABC::obj1 : "<<sizeof(obj1);

    ClassXYZ obj2;
    cout<<"\n Size of ClassXYZ::obj2 : "<<sizeof(obj2);
}


Output=========================
Size of ClassABC::obj1 : 8
Size of ClassXYZ::obj2 : 12
===============================

Even though the derived class doesn't inherit the vtable pointer, the derived class still inherits virtual member functions and so it gets a vtable pointer anyway.
Size of ClassABC
4 bytes for ClassABC own virtual table pointer
4 bytes for ClassABC own data

Size of ClassXYZ
4 bytes for ClassXYZ own virtual table pointer
4 bytes for inherited ClassABC data
4 bytes for ClassXYZ own data

ClassXYZ has a virtual function (intherited from ClassABC), so it needs a vtable.
Last edited on

First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it’s own virtual table.

Second, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it’s own virtual pointer.



All clear now. :)
First, every class that uses virtual functions (or is derived from a class that uses virtual functions) is given it’s own virtual table.
Yes. Virtualness is poisonous: once you have virtual table, you and your children will always have it.

Second, every instance of class that uses virtual functions (or is derived from a class that uses virtual functions) is given it’s own virtual pointer.
There is one virtual table per class, and every instance of class holds a pointer to it. This is done to not clutter classes: if there is 15 virtual functions, there would be 15 entries in class virtual table, but each instance holds only a single pointer to it and not whole table.
Right.........
Topic archived. No new replies allowed.