Re: memory usage of Virtual function poiters
From: David White (no_at_email.provided)
Date: 05/07/04
- Next message: David White: "Re: memory usage of Virtual function poiters"
- Previous message: Mark : "Re: Attempt at fixing the issue."
- In reply to: Paul: "memory usage of Virtual function poiters"
- Next in thread: David White: "Re: memory usage of Virtual function poiters"
- Reply: David White: "Re: memory usage of Virtual function poiters"
- Reply: Paul: "Re: memory usage of Virtual function poiters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: David White: "Re: memory usage of Virtual function poiters"
- Previous message: Mark : "Re: Attempt at fixing the issue."
- In reply to: Paul: "memory usage of Virtual function poiters"
- Next in thread: David White: "Re: memory usage of Virtual function poiters"
- Reply: David White: "Re: memory usage of Virtual function poiters"
- Reply: Paul: "Re: memory usage of Virtual function poiters"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|