ahuttune@niksula.hut.fi (Ari Juhani Huttunen) (08/22/90)
Consider an example class:
class X {
public:
virtual a(void) = 0;
virtual b(void) = 0;
};
Just how are these "virtual function tables" constructed? (By the compiler..)
Are the ONE or TWO tables? And to come to the point of this article, are
there ONE or TWO pointers to these tables? So, do my classes get cluttered
with implicit pointers if I put many virtual functions in them?
My guess is that there is just ONE pointer, but is it enough if you write
MANY virtual functions?
--
___ ___ ___ ___ ___ ___ ___ ___ ___
__I I__I I__I I__I I__I I__I I__I I__I I__I I Thank you
Ari Huttunen (ahuttune@niksula.hut.fi) I for not smoking!
____________________________________________I <Robocop>
steve@taumet.com (Stephen Clamage) (08/24/90)
ahuttune@niksula.hut.fi (Ari Juhani Huttunen) writes: >Consider an example class: >class X { >public: > virtual a(void) = 0; > virtual b(void) = 0; >}; >Just how are these "virtual function tables" constructed? (By the compiler..) >Are the ONE or TWO tables? And to come to the point of this article, are >there ONE or TWO pointers to these tables? So, do my classes get cluttered >with implicit pointers if I put many virtual functions in them? This is an implementation issue, as opposed to a language definition issue, since nothing in the language definition requires virtual tables at all. But all implementations with which I am familiar follow this model: Single inheritance: Every object of a class with virtual functions contains one pointer to a table of virtual functions. The more virtual functions, the bigger the table is, but there is only one pointer to it per object. A derived-class object points to a table containing addresses of non-overridden base class virtual functions, as well as its own virtual functions. For each class type with virtual functions there need be only one instance of a virtual table in the entire program, and all objects of that type point to it. (The contents of a vtable never change dynamically, so one copy serves for all objects of the type.) Multiple inheritance: When a pointer to an object is passed to a function expecting a pointer to a base class, the resulting object must look like the base class object. This means that a class directly derived from more than one base class must contain a virtual table pointer for each of its base classes which has virtual functions. Usually one of the base classes will be located at the start of the object, and the object's virtual table pointer can be the same as that base class's vtable pointer. So for multiple inheritance, there is a small space penalty for the extra vtable pointers. (There are also other time and space penalties for multiple inheritance.) -- Steve Clamage, TauMetric Corp, steve@taumet.com