From: red floyd on
On Aug 10, 1: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.

Consider:

#include <iostream>
#include <ostream>
class A {
public:
virtual void f() {
std::cout << "A::f" << std::endl;
}
};

class B : public A {
public:
virtual void f() {
std::cout << "B::f" << std::endl;
}
};

void func(A* a)
{
a->f();
}

int main()
{
A a;
B b;

func(&a);
func(&b);
}

How would the compiler know which "static vtbl" to use in func() when
calling a->f()?

By having each object of a class contain a pointer to it's vtbl (vptr),
then the polymorphism is easily implemented.

DISCLAIMER: Remember that the Standard does not require a vptr or vtbl
(as you noted in your post, it's an implementation detail).


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

From: Felix Palmen on
* MC <manan.chopra(a)gmail.com>:
> 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.

Think again what exactly a vtable is good for. Say X is derived from Y
and you do
Y *c = &b;
c->foo();
where foo is a virtual function actually overridden in X. What would be
called?

Regards, Felix

--
Felix Palmen (Zirias) + [PGP] Felix Palmen <felix(a)palmen-it.de>
web: http://palmen-it.de/ | http://palmen-it.de/pub.txt
my open source projects: | Fingerprint: ED9B 62D0 BE39 32F9 2488
http://palmen-it.de/?pg=pro + 5D0C 8177 9D80 5ECF F683

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

From: Chris Uzdavinis 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.

Consider this program, and ask yourself how the print() function
can possibly work with your suggested implementation.

#include <iostream>

class Base {
public:
virtual void foo() = 0;
virtual ~Base() {}
};

class Derived1 : public Base {
public:
virtual void foo() { std::cout << "Derived1" << std::endl; }
};

class Derived2 : public Base {
public:
virtual void foo() { std::cout << "Derived2" << std::endl; }
};

void print(Base const & base)
{
base.foo();
}

int main()
{
Derived1 d1;
Derived2 d2;
print(d1);
print(d2);
}

The problem is, of course, that while print() works on Base objects,
that's the static type of the object, but the dynamic type is really
*either* Derived1 or Derived2, so in the exact same function, print()
needs to invoke 2 different foo() functions, without knowing the type
of the object on which it's calling.

Thus, d1 and d2 have different vtbl pointers, and inside print(),
the "right" function is called by looking at the object's vtbl,
without
caring what type it is. (Furthermore, in the context of print(),
the static type of the object is wrong, as it's an abstract type,
so it would not be possible to access the data the way you suggest.)

--
Chris

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

From: CornedBee on
On Aug 10, 1: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.

What about the X subobject of a class Y that derives from it? The vptr
of those subobjects points to Y's vtable. The entire concept of
dynamic types in a polymorphic hierarchy comes down to "where does the
vptr point?".

Sebastian


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