[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

vtbl and vptr

Thomas Lenz

12/16/2008 7:22:00 AM

Hi group,

just curious: assuming i have a base class B with virtual functions and
other classes derived from B:

does this result in a vtbl / vptr's per class, or per object ?


thanks, Thomas

20 Answers

Ripunjay Tripathi

12/16/2008 7:28:00 AM

0

On Dec 16, 12:22 pm, Thomas Lenz <len...@gmx.de> wrote:
> Hi group,
>
> just curious: assuming i have a base class B with virtual functions and
> other classes derived from B:
>
> does this result in a vtbl / vptr's per class, or per object ?
>
> thanks, Thomas


pointer to vtable is inserted by compiler per-object

Regards,
Ripunjay Tripathi

Juan Antonio Zaratiegui Vallecillo

12/16/2008 8:16:00 AM

0

Thomas Lenz escribió:
> Hi group,
>
> just curious: assuming i have a base class B with virtual functions and
> other classes derived from B:
>
> does this result in a vtbl / vptr's per class, or per object ?
>
>
> thanks, Thomas
>

The information that belongs to the class will exits per class. *If*
this is implemented as a vtbl, there will be a vtbl per class.

Each object needs to know the class it belongs to, and thus it will
contain some way to do that. *If* this is implemented as a vptr, there
will be a vptr per object.

There may be other ways to solve virtuality problems, but always "...
give to Caesar what is Caesar's , and to God what is God's". That is,
information belonging to a class will exist in a per-class basis
(similar to an static data member), information belonging to an object
will exist within the object (similar to a non-static data member)

Best regards,

Zara

Thomas Lenz

12/16/2008 9:58:00 AM

0

am Dienstag 16 Dezember 2008 09:16 schrieb Juan Antonio Zaratiegui
Vallecillo:

> Thomas Lenz escribió:
>> Hi group,
>>
>> just curious: assuming i have a base class B with virtual functions and
>> other classes derived from B:
>>
>> does this result in a vtbl / vptr's per class, or per object ?
>>
>>
>> thanks, Thomas
>>
>
> The information that belongs to the class will exits per class. *If*
> this is implemented as a vtbl, there will be a vtbl per class.
>
> Each object needs to know the class it belongs to, and thus it will
> contain some way to do that. *If* this is implemented as a vptr, there
> will be a vptr per object.
>
> There may be other ways to solve virtuality problems, but always "...
> give to Caesar what is Caesar's , and to God what is God's". That is,
> information belonging to a class will exist in a per-class basis
> (similar to an static data member), information belonging to an object
> will exist within the object (similar to a non-static data member)
>
> Best regards,
>
> Zara

ok, so it's one vtbl per class.

Just to make sure i got the vptr issue right: my understanding so far is
that vptrs point to the right virtual function, based on the "real" object
type, not on the type of the pointer/reference that points/references to
the object. So is there one vptr per object PER (VIRTUAL) FUNCTION? or do
compilers (assuming they use the vptr approach) somehow manage to do this
with just one vptr per object (only)?

Thanks in advance,
Thomas

Juan Antonio Zaratiegui Vallecillo

12/16/2008 11:16:00 AM

0

Thomas Lenz escribió:
> am Dienstag 16 Dezember 2008 09:16 schrieb Juan Antonio Zaratiegui
> Vallecillo:
>
>> Thomas Lenz escribió:
>>> Hi group,
>>>
>>> just curious: assuming i have a base class B with virtual functions and
>>> other classes derived from B:
>>>
<...>

> ok, so it's one vtbl per class.
>
> Just to make sure i got the vptr issue right: my understanding so far is
> that vptrs point to the right virtual function, based on the "real" object
> type, not on the type of the pointer/reference that points/references to
> the object. So is there one vptr per object PER (VIRTUAL) FUNCTION? or do
> compilers (assuming they use the vptr approach) somehow manage to do this
> with just one vptr per object (only)?
>

Usually, the vptr will be a pointer to the class vtbl. Such vtbl will
contain:

* typeid info.
* info to resolve dynamic_cast
* pointers to virtual functions, as overloaded for this particular
class. Only one pointer indirection should be necessary to apply the
function.

Remember this info is not the only information 'virtualised', you should
also take virtual base classes into account.

best regards,

Zara

Rolf Magnus

12/16/2008 12:26:00 PM

0

Thomas Lenz wrote:

> am Dienstag 16 Dezember 2008 09:16 schrieb Juan Antonio Zaratiegui
> Vallecillo:
>
>> Thomas Lenz escribió:
>>> Hi group,
>>>
>>> just curious: assuming i have a base class B with virtual functions and
>>> other classes derived from B:
>>>
>>> does this result in a vtbl / vptr's per class, or per object ?
>>>
>>>
>>> thanks, Thomas
>>>
>>
>> The information that belongs to the class will exits per class. *If*
>> this is implemented as a vtbl, there will be a vtbl per class.
>>
>> Each object needs to know the class it belongs to, and thus it will
>> contain some way to do that. *If* this is implemented as a vptr, there
>> will be a vptr per object.
>>
>> There may be other ways to solve virtuality problems, but always "...
>> give to Caesar what is Caesar's , and to God what is God's". That is,
>> information belonging to a class will exist in a per-class basis
>> (similar to an static data member), information belonging to an object
>> will exist within the object (similar to a non-static data member)
>>
>> Best regards,
>>
>> Zara
>
> ok, so it's one vtbl per class.
>
> Just to make sure i got the vptr issue right: my understanding so far is
> that vptrs point to the right virtual function,

No. They point to the vtable.

> based on the "real" object type, not on the type of the pointer/reference
> that points/references to the object. So is there one vptr per object PER
> (VIRTUAL) FUNCTION? or do compilers (assuming they use the vptr approach)
> somehow manage to do this with just one vptr per object (only)?

Yes, they do. That is actually what the vtable is for. It's (mostly) a table
of pointers to all the virtual functions in the class.

Rolf Magnus

12/16/2008 12:38:00 PM

0

Juan Antonio Zaratiegui Vallecillo wrote:

> Usually, the vptr will be a pointer to the class vtbl. Such vtbl will
> contain:
>
> * typeid info.
> * info to resolve dynamic_cast
> * pointers to virtual functions, as overloaded for this particular
> class. Only one pointer indirection should be necessary to apply the
> function.

Actually, it's at least 3 (might be more with multiple inheritance or
virtual inheritance). When doing a polymorphic call, you have a pointer to
the object to begin with. You need to dereference that to get to the vtable
pointer. Dereferencing that brings you to the pointer to the virtual
function to be called. And then this pointer needs to be dereferenced for
the actual function call.

Thomas Lenz

12/16/2008 1:26:00 PM

0

am Dienstag 16 Dezember 2008 13:37 schrieb Rolf Magnus:

> Juan Antonio Zaratiegui Vallecillo wrote:
>
>> Usually, the vptr will be a pointer to the class vtbl. Such vtbl will
>> contain:
>>
>> * typeid info.
>> * info to resolve dynamic_cast
>> * pointers to virtual functions, as overloaded for this particular
>> class. Only one pointer indirection should be necessary to apply the
>> function.
>
> Actually, it's at least 3 (might be more with multiple inheritance or
> virtual inheritance). When doing a polymorphic call, you have a pointer to
> the object to begin with. You need to dereference that to get to the
> vtable pointer. Dereferencing that brings you to the pointer to the
> virtual function to be called. And then this pointer needs to be
> dereferenced for the actual function call.

Thanks Juan and Rolf for this detailed explanation! It helps me a lot to get
some deeper understanding of this virtual stuff...

Best regards,
Thomas

red floyd

12/16/2008 4:21:00 PM

0

On Dec 15, 11:22 pm, Thomas Lenz <len...@gmx.de> wrote:
> Hi group,
>
> just curious: assuming i have a base class B with virtual functions and
> other classes derived from B:
>
> does this result in a vtbl / vptr's per class, or per object ?

Technically, vtbl/vptr is an implementation detail. There is no
requirement
that a class even *have* a vtbl or vptr. I can imagine a naive (and
not particularly
efficient) implementation without one.


Laurent Deniau

12/16/2008 5:01:00 PM

0

On 16 déc, 12:16, Juan Antonio Zaratiegui Vallecillo <z...@coit.es>
wrote:
> Thomas Lenz escribió:> am Dienstag 16 Dezember 2008 09:16 schrieb Juan Antonio Zaratiegui
> > Vallecillo:
>
> >> Thomas Lenz escribió:
> >>> Hi group,
>
> >>> just curious: assuming i have a base class B with virtual functions and
> >>> other classes derived from B:
>
> <...>
>
> > ok, so it's one vtbl per class.
>
> > Just to make sure i got the vptr issue right: my understanding so far is
> > that vptrs point to the right virtual function, based on the "real" object
> > type, not on the type of the pointer/reference that points/references to
> > the object. So is there one vptr per object PER (VIRTUAL) FUNCTION? or do
> > compilers (assuming they use the vptr approach) somehow manage to do this
> > with just one vptr per object (only)?
>
> Usually, the vptr will be a pointer to the class vtbl. Such vtbl will
> contain:
>
> * typeid info.
> * info to resolve dynamic_cast

and more like MI/VI offsets (thunks) and partial ctors/dtors for
classes inheriting from virtual base classes

> * pointers to virtual functions, as overloaded

I would say "as overridden" (overloading is related to types).

> for this particular
> class. Only one pointer indirection should be necessary to apply the
> function.

actually, 3 indirections and more with offsets adjustment for MI/VI.

> Remember this info is not the only information 'virtualised', you should
> also take virtual base classes into account.

which leads to more vtables.

For example assuming the two polymorphic classes B and D:

class B { ... };
class D : virtual B { ... };

D should have two vtables, two ctors for each "user" defined ctor and
instances should hold two vptrs.

regards,

ld.

Laurent Deniau

12/16/2008 5:04:00 PM

0

On 16 déc, 17:20, red floyd <redfl...@gmail.com> wrote:
> On Dec 15, 11:22 pm, Thomas Lenz <len...@gmx.de> wrote:
>
> > Hi group,
>
> > just curious: assuming i have a base class B with virtual functions and
> > other classes derived from B:
>
> > does this result in a vtbl / vptr's per class, or per object ?
>
> Technically, vtbl/vptr is an implementation detail.  There is no
> requirement
> that a class even *have* a vtbl or vptr.  I can imagine a naive (and
> not particularly
> efficient) implementation without one.

could you describe it shortly?

regards,

ld.