From: MC on
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,

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

From: Goran Pusic 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.

Me, too ;-)

> 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.

Ask yourself this: how will object know which vptr table to take? It
has to know that it's type is X, and hence, when virtual function f is
called on it, it has to do (metacode):

GENERIC_CLASS_STATIC_DATA& sd = this->get_generic__static_data(); ^^^
vptr* p = sd.vptr();
p[index_of_f](this, params);

How do you plan to do ^^^ (should resolve to e.g. X::static_data)?
Solution is exactly that: store some sort of indication of object's
type __in the object__. And that indication is virtual table pointer.
(And when you compile with RTTI on, in the vicinity of said vptr table
you have object's RTTI, too.)

In a way, what you are asking for __is__ what is happening, you are
just looking at things with different eyes. :-)

Goran.


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

From: Bo Persson 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.
>

The vtable is interesting only when you have other classes, like Y and
Z, derivning from X. Then a pointer to X could in fact point to an
object of either X, Y, or Z. In that case, the vtable will be used to
select the proper virtual function to call.

If you only have X objects, many compiler will bypass the vtable and
call X functions directly.


Bo Persson


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

From: Thomas Richter on
MC schrieb:
> 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.

Likely, but quite irrelevant. The C++ standard doesn't say how to
implement dynamic dispatch and polymorphism.

> 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 it is one way of how the compiler can deduce the dynamic type of
an object.

> 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.

Because then polymorphism wouldn't work. If you, at some point, hold a
pointer to a class derived from the class of a or b, then this pointer
may have the same static type, i.e. pointer to the base class. However,
the object it points to is actually of the derived class.

Static members are, however, resolved based on the static type, and thus
you would get the wrong "vtable". Actually, the purpose of the vtable
implmentation strategy is exactly that: Storing some information on the
object in the object without requiring static type information deduced
at compile time - and this is what is required for polymorphism.

So long,
Thomas

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

From: Ulrich Eckhardt on
MC wrote:
> 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?

That's not true, objects of the same class can have different vtables. This
happens if they are not exactly of that class but of a derived class.

If you have a debugger that shows the vtable pointer, you could output the
pointer in the base-class constructor and then again in the derived class
constructor for the same object. You would then see that the same pointer
has different values. This also means the value changes during construction
and destruction.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


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