 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
M.S.Zubeir Guest
|
Posted: Mon Jul 12, 2004 9:38 pm Post subject: Virtual table content |
|
|
Hi All,
Suppose I have a parent class P having a virtual function and two
drived classes D1 & D2.
If I created two pointers of P and assigned them with object of D1 and
object of D2 and both are active in the same scope, what will be the
content of P's VTable? (whether D1's content or D2's content or both
will be available there?).
Let me know your kind view on this.
Thanks,
M.S.Zubeir
[email]mail2zubeir (AT) yahoo (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Tue Jul 13, 2004 10:45 am Post subject: Re: Virtual table content |
|
|
M.S.Zubeir wrote:
| Quote: | If I created two pointers of P and assigned them with object of D1 and
object of D2 and both are active in the same scope, what will be the
content of P's VTable? (whether D1's content or D2's content or both
will be available there?).
|
You are confused. Objects contain pointers to vtables (in the common
implementation). A D1 object contains a pointer to a D1 vtable, and
likewise for D2. The P pointers just point to the Di objects. There
is no "P's vtable" here, so your question is meaningless.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Tue Jul 13, 2004 11:05 am Post subject: Re: Virtual table content |
|
|
M.S.Zubeir wrote:
| Quote: | Hi All,
Suppose I have a parent class P having a virtual function and two
drived classes D1 & D2.
If I created two pointers of P and assigned them with object of D1 and
object of D2 and both are active in the same scope, what will be the
content of P's VTable? (whether D1's content or D2's content or both
will be available there?).
|
The contents of the virtual table, or whatever mechanism is being
used for dynamic binding, is defined at compile time. Each of your
classes will have one of these things, giving you a total of three.
An instance of class that has virtual functions contains a reference
(or pointer) to the virtual table, which it uses for dynamic dispatch
of functions.
See Lippman's "The C++ Object Model" for a detailed explanation of
one possible implementation. Also, see the assembly code output
from your compiler, you'll see how your particular implementation
does it.
--
Antoun Kanawati
[email]antounk.at (AT) comcast (DOT) dot.net[/email]
[remove .dot and .at before use]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Tue Jul 13, 2004 8:39 pm Post subject: Re: Virtual table content |
|
|
In article <86516f95.0407120346.66bf574e (AT) posting (DOT) google.com>, M.S.Zubeir
<mail2zubeir (AT) yahoo (DOT) com> writes
| Quote: | Hi All,
Suppose I have a parent class P having a virtual function and two
drived classes D1 & D2.
If I created two pointers of P and assigned them with object of D1 and
object of D2 and both are active in the same scope, what will be the
content of P's VTable? (whether D1's content or D2's content or both
will be available there?).
|
With the caveat that there is no requirement to use a vtable in C++:
Your code will have three vtables, one for P, one for D1 and one for D2.
Each object of a type that has a vtable has an extra data member which
is a pointer to that objects vtable. I.e. the cost of polymorphism is
one vtable per polymorphic type and one pointer per instance.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Richter Guest
|
Posted: Tue Jul 13, 2004 8:48 pm Post subject: Re: Virtual table content |
|
|
Hi,
| Quote: | Suppose I have a parent class P having a virtual function and two
drived classes D1 & D2.
If I created two pointers of P and assigned them with object of D1 and
object of D2 and both are active in the same scope, what will be the
content of P's VTable? (whether D1's content or D2's content or both
will be available there?).
|
First of all, a vtable is an implementation detail. What's important is
that the right member function is called, namely that of D1 or D2, and not
that of the base class P, even though your pointer is of type P*.
To answer your question, the content of the P vtable is of course pointers
to member functions of the P class, though that's pretty irrelevant since you
don't have any P objects here. You've a D1 and a D2, and thus, the contents
of the vtable your pointers point to will be pointers to member functions of
D1 resp. D2 (or the base, if not overloaded).
So long,
Thomas
P.S.: Pointer to member function is here meant to understood as "the call-in
address of the method", not in the sense of the C++ construct "pointer to
member function" which is actually an object/function pointer pair.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Tue Jul 13, 2004 9:08 pm Post subject: Re: Virtual table content |
|
|
[email]mail2zubeir (AT) yahoo (DOT) com[/email] (M.S.Zubeir) wrote in message
news:<86516f95.0407120346.66bf574e (AT) posting (DOT) google.com>...
| Quote: | Suppose I have a parent class P having a virtual function and two
drived classes D1 & D2.
If I created two pointers of P and assigned them with object of D1 and
object of D2 and both are active in the same scope, what will be the
content of P's VTable? (whether D1's content or D2's content or both
will be available there?).
|
The compiler can do whatever it wants, as long as calls to virtual
functions in P go to the right class.
In practice, in the implementations I'm familiar with, P's vtable will
correspond to the case where P is the most derived class. And it won't
be used in the above scenario (except during the construction of the
objects). Typically, each object will contain a pointer to the vtable
(a vptr), which is initialized in the constructor. This vptr is at a
fixed address within the object -- often, at the start. In your case,
in the D1 object, it will point to D1's vtable, and in the D2 object, it
will point to D2's vtable. A call to a virtual function will start by
getting the vptr from the object, and looking up the function address
there.
In the case of multiple heritage, there are additional complications, of
course: the object will typically contain several vptr, and the compiler
will do address fix-up on the this pointer (based on information in the
vtable) when calling the function.
--
James Kanze GABI Software http://www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
M.S.Zubeir Guest
|
Posted: Fri Jul 23, 2004 2:18 pm Post subject: Re: Virtual table content |
|
|
Antoun Kanawati <antounk (AT) comcast (DOT) net> wrote
| Quote: | M.S.Zubeir wrote:
Hi All,
Suppose I have a parent class P having a virtual function and two
drived classes D1 & D2.
If I created two pointers of P and assigned them with object of D1 and
object of D2 and both are active in the same scope, what will be the
content of P's VTable? (whether D1's content or D2's content or both
will be available there?).
The contents of the virtual table, or whatever mechanism is being
used for dynamic binding, is defined at compile time. Each of your
classes will have one of these things, giving you a total of three.
An instance of class that has virtual functions contains a reference
(or pointer) to the virtual table, which it uses for dynamic dispatch
of functions.
See Lippman's "The C++ Object Model" for a detailed explanation of
one possible implementation. Also, see the assembly code output
from your compiler, you'll see how your particular implementation
does it.
|
Hi,
The virtual mechanism is meant for dynamic binding only.So how come it
be defined at compile time ? Moreover, if all the three instances
occupy the table at one time means how can the correct function get
mapped for a call?
Zubeir.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Jul 23, 2004 3:45 pm Post subject: Re: Virtual table content |
|
|
In article <86516f95.0407230131.1517a10a (AT) posting (DOT) google.com>, M.S.Zubeir
<mail2zubeir (AT) yahoo (DOT) com> writes
| Quote: | Hi,
The virtual mechanism is meant for dynamic binding only.So how come it
be defined at compile time ? Moreover, if all the three instances
occupy the table at one time means how can the correct function get
mapped for a call?
|
It seems that you really do not understand the concept of dynamic
binding and how it is usually supported in C++.
In C++ every object has a type that is identified at compile time (its
static type). Part of the implementation of static types that are part
of a polymorphic hierarchy is that in addition to the user specified
data members it includes an extra 'invisible' data member which is a
pointer to the vtable for that static type. Every static type that
utilises dynamic binding has a table of virtual function pointers that
point to the virtual member functions for that static type.
Note that all the above is an implementation detail and not specified by
the C++ Standard (which only specifies results, not the way those
results are obtained)
Objects can be accessed by references or pointers. In such case we have
two types involved, the type of the pointer (often called the static
type) and the actual type of the object currently referenced or pointed
to. In these cases choice of which overrider of a virtual function is
delayed until the type static type of the object being accessed (by a
pointer or reference) is known at which point the relevant function
pointer is used from the static table relevant to the static type of the
object which gives the dynamic type of the pointer or reference. That is
called dynamic binding.
Confused? Then stop worrying about it because it works and that is the
only thing that the application level programmer needs to be concerned
with.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sat Jul 24, 2004 12:01 am Post subject: Re: Virtual table content |
|
|
M.S.Zubeir wrote:
| Quote: | Antoun Kanawati <antounk (AT) comcast (DOT) net> wrote
The contents of the virtual table, or whatever mechanism is being
used for dynamic binding, is defined at compile time. Each of your
classes will have one of these things, giving you a total of three.
An instance of class that has virtual functions contains a reference
(or pointer) to the virtual table, which it uses for dynamic dispatch
of functions.
See Lippman's "The C++ Object Model" for a detailed explanation of
one possible implementation. Also, see the assembly code output
from your compiler, you'll see how your particular implementation
does it.
Hi,
The virtual mechanism is meant for dynamic binding only.So how come it
be defined at compile time ? Moreover, if all the three instances
occupy the table at one time means how can the correct function get
mapped for a call?
|
You really should look at Lippman's book; the diagrams and the
explanations are excellent. It answers all your questions.
However, it seems that you're focussing on a detail of implementation
before having acquired solid grasp of dynamic binding.
Abstractly speaking, the primarhy difference between static binding
and dynamic bindings is what you know at compile time.
Given a statically bound call, you can, by simply reading the code,
tell exactly what that call will invoke, and you will have exactly
one answer in the form of StaticType::FunctionName.
Given a dynamically bound, through a reference or a pointer, you only
know a partial name of the function; that is: of the Class::FunctionName
pair, only FunctionName is defined at compile time, while Class depends
on the dynamic type of the object being used to invoke the function.
Hence, the dynamic binding mechanism must acquire the "Class" info from
the object itself, so that it can compute an answer.
At the mechanism level, the primary difference between static binding
and dynamic binding is what the compiler generates for a function call.
For statically bound methods, the compiler generates a simple direct
call to the function.
For a dynamically bound method on an object, the compiler generates
an indirect function call, through a function pointer that depends on
the dynamic type of the object. The dynamic type of the object
determines which "virtual table" should be used; the static type of
the object determines how that table is used. Usually, the static type
of the object maps the function name to a constant, which is used as
an index into a data structure associated with the dynamic type of
the object.
The precise manner in which an object is associated with its dynamic
type information varies among implementations, but ultimately, it
involves a special pointer embedded in the instance that points to
that information. Generally, the data structure at the destination end
of the pointer is called the "virtual table" or "vtable"; the term
refers to the original implementation, in CFront, of dynamic binding,
which used a special hidden member calld "_vtbl" and introduced the
terms "virtual table" and "vtable" into the lingo through its
documentation, and diagnostic messages.
--
Antoun Kanawati
[email]antounk.at (AT) comcast (DOT) dot.net[/email]
[remove .dot and .at before use]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|