Re: memory usage of Virtual function poiters

From: David White (no_at_email.provided)
Date: 05/07/04


Date: Fri, 7 May 2004 08:48:11 +1000


"Paul" <paulmmm01@hotmail.com> wrote in message
news:5dab091d.0405061333.411142dd@posting.google.com...
> Hi there I have a question about the above subject.
>
> (phsuedo code))
> Say we have a class with 20 odd member variables i.e:
> class TheClass{
> int i1;
> char i2;
> double i3;
> bool i4;
> float i5;
> /*etc*/
>
> void foo(int){}
> void foo(char){}
> void foo(double){}
> void foo(bool){}
> void foo(float){}
> /*etc*/
> };
>
> And as an alternative we had another class which had 20 virtual
> functions like so:
> class Base{
> virtual void foo(int){}
> virtual void foo(char){}
> virtual void foo(double){}
> virtual void foo(bool){}
> virtual void foo(float){}
> /* etc*/
> };
>
> template<class C>
> class TheClass: public Base{
> template<typename T>
> void foo(T){}
> C _c;
> };
>
>
> In the second example there is only one variable needed but is there a
> cost for having the virtual function table?

It's up to the compiler how to implement virtual functions, but typically
there'll be a single, hidden pointer member in each object for a case such
as the above. The pointer points to table of pointers, each of which is the
address of the function to call for each defined virtual function. There
will be one such pointer table for each _class_ (not object) that has or
inherits any virtual functions.

> For example if I were to create a large array of TheClass's would I be
> better of using the first method or the second or is there no real
> difference? Does the virtual function table cancel out what you appear
> to be saving on variables?

The second version is almost certainly more memory efficient. However, I'm
missing how the second version is the equivalent of the first. The first
version supports all of the foo overloads, but the second version seems to
support only one.

DW



Relevant Pages

  • Re: memory usage of Virtual function poiters
    ... >void foo ... have an extra hidden data pointer (the pointer to the virtual function ... member which begs the question as to why you had 20 originally. ... but also how could this design meet the same needs as the previous ...
    (alt.comp.lang.learn.c-cpp)
  • Interview Questions
    ... What is a pure virtual function, and why would you use it? ... class Super1: public Sub ... void DoSomething ... while (cur!= SubVector.end()) ...
    (microsoft.public.vc.mfc)
  • Re: Hmm... inheritence... hmmm
    ... is a class Dog or any derived from it. ... would work without throwing a bad_cast exception. ... types are compatible, then calls the virtual function of the class, which ... void mate{ ...
    (comp.lang.cpp)
  • Re: Sorting a CList
    ... You are correct about the virtual function requirement. ... use dynamic_cast on a pointer to pointer of type X**, ... cannot cast from void*, so when you start from void*, as the sort ... will have this initial unchecked cast at the very least, ...
    (microsoft.public.vc.mfc)
  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)