From: Bart van Ingen Schenau on
On Aug 10, 10:20 pm, MC <manan.cho...(a)gmail.com> wrote:
> Hi!
> I have another naive question. I have been trying to understand the
> polymorphysim implementation in C++.
> I believe that most of the compilers implement it using vptr and
> vtables.
> My question is when for every object of a class (which has a virtual
> member) if vptr points to the same vtable, why does every object has
> a copy of the same vptr?

Because derived classes will re-use the vptr from their base class to
store a pointer to their own vtable.

>
> For example
> say X is a class which has a virtual function
> and a and b are objects of that class
> both a and b will have a vptr installed in them and this vptr will
> point to the same vtable
> Why not have the vptr as a static data member of the class X so that
> every object of that class can use it.

Suppose there is a second class Y, that is derived from X and c is an
object of class Y.
Now you also have a pointer p (of type X*) that can refer either to b
or to c and you want to call the virtual function through p.
How does the compiler know which vptr/vtable to use? The vptr/vtable
from X is not appropriate if p refers to c, but where the call is
made, the compiler does not even know that Y exists.

That is the reason that each object carries around a vptr. That allows
the compiler to lookup the correct vtable at a call site, without even
knowing about all the possible derived classes.

The whole vtable/vptr mechanism only exists to facilitate indirect
access to virtual members (through a pointer or reference).
With direct access, the compiler has all the information it needs to
determine exactly which function to call, but with indirect access,
there might be as of yet unknown derived classes in play.

Bart v Ingen Schenau


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Eddie on
On Aug 10, 3:20 pm, MC <manan.cho...(a)gmail.com> wrote:
> Hi!
> I have another naive question. I have been trying to understand the
> polymorphysim implementation in C++.
> I believe that most of the compilers implement it using vptr and
> vtables.
> My question is when for every object of a class (which has a virtual
> member) if vptr points to the same vtable, why does every object has
> a copy of the same vptr?
>
> For example
> say X is a class which has a virtual function
> and a and b are objects of that class
> both a and b will have a vptr installed in them and this vptr will
> point to the same vtable
> Why not have the vptr as a static data member of the class X so that
> every object of that class can use it.
>
> I am sure there is a very good reason for the way it is implemented,
> but because of my inexperience with C++ I dont see the reason. Can
> anybody please elaborate on the reason.
>
> Thanks,

{ quoted clc++m banner removed; please do it yourself. -mod }

I believe the reason why a vptr is stored per object is for caching
reasons. If it is static like you proposed, the CPU would have to
fetch the data from global space and send it to cache. But since we
are accessing the object anyhow, all data (including the vptr) should
be fresh in the cache.

Please correct me if my assumption is wrong.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Nick Hounsome on
On 10 Aug, 21:20, MC <manan.cho...(a)gmail.com> wrote:
> Hi!
> I have another naive question. I have been trying to understand the
> polymorphysim implementation in C++.
> I believe that most of the compilers implement it using vptr and
> vtables.
> My question is when for every object of a class (which has a virtual
> member) if vptr points to the same vtable, why does every object has
> a copy of the same vptr?
>
> For example
> say X is a class which has a virtual function
> and a and b are objects of that class
> both a and b will have a vptr installed in them and this vptr will
> point to the same vtable
> Why not have the vptr as a static data member of the class X so that
> every object of that class can use it.
>
> I am sure there is a very good reason for the way it is implemented,
> but because of my inexperience with C++ I dont see the reason. Can
> anybody please elaborate on the reason.

That's a chicken and egg problem.
How do the objects know what type they are?
It's true that if you write:

X a;
a.SomeVirtualXfunction();

Then the compiler will just call the function bypassing the vtbl
because it KNOWS that a REALLY IS an X but:

void f(X& a)
{
a.SomeVirtualXfunction();
}

Is a realy an X or is it derived?
WHo knows? But its vptr will either point to X::SomeVirtualXfunction()
or Derived::SomeVirtualXfunction().
Regarding the actual, most derived type of a - that is held in the
vtbl too.

If you're realy interested in this stuff I suggest that you get hold
of a copy of "THe annotated C++ reference manual" it's out of date but
it explains all this stuff realy well.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
MC wrote:
> Hi!
> I have another naive question. I have been trying to understand the
> polymorphysim implementation in C++.
> I believe that most of the compilers implement it using vptr and
> vtables.
> My question is when for every object of a class (which has a virtual
> member) if vptr points to the same vtable, why does every object has
> a copy of the same vptr?
>
> For example
> say X is a class which has a virtual function
> and a and b are objects of that class
> both a and b will have a vptr installed in them and this vptr will
> point to the same vtable
> Why not have the vptr as a static data member of the class X so that
> every object of that class can use it.
>
> I am sure there is a very good reason for the way it is implemented,
> but because of my inexperience with C++ I dont see the reason. Can
> anybody please elaborate on the reason.
>
> Thanks,
>

You could do it that way but then every object would have to have a
member that identified its dynamic type so that references and pointers
work correctly. In other words every object must have a way of knowing
its type. In most (probably all) current implementations the vptr does
exactly that.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Jens Schmidt on
MC wrote:

> My question is when for every object of a class (which has a virtual
> member) if vptr points to the same vtable, why does every object has
> a copy of the same vptr?
>
> For example
> say X is a class which has a virtual function
> and a and b are objects of that class
> both a and b will have a vptr installed in them and this vptr will
> point to the same vtable
> Why not have the vptr as a static data member of the class X so that
> every object of that class can use it.

A static data member exists only once. Not once for each class, but once
for each class tree.
So the static data member of a class Y derived from X and overriding the
virtual function is just the same as the one in X.
It can't have a different value, so the classes can't have different
vtables. This contradicts the initial assumption, which is therefore proven
false.
--
Greetings,
Jens Schmidt


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]