[geeks] C++ inner workings

Joshua D Boyd geeks at sunhelp.org
Fri Nov 23 14:09:02 CST 2001


On Thu, Nov 22, 2001 at 01:42:14PM -0500, Big Endian wrote:
> ><snip>
> >How does it know which read to call?  I can think of some possible ways to
> >implement this functionality, but I want to know what actually occurs.
> 
> Look up "virtual function tables"  in the GCC doc or somewhere like that.

I've been searching the net (lots of references, but I didn't find anything
actually usefull till page 8 of the goodle search).  Let me post URLs for the
two most usefull things I found:
http://www.cuj.com/experts/1905/hyslop.htm?topic=experts
http://www.develop.com/hp/slip/om.pdf

To summarize, the C++ spec. doesn't specify how it is handled, but the 
committie in charge apparantly has a codified standard practice.  Assuming 
that GCC mainly adheres to that practice, then if I have:
class parent { virtual void method(); }
class child1: parent { void method(); }
class child2: parent { ;} 
the layout in memory for a new instance of the child1 class is probably 
something like:
parent: pointer to virtual function table
parent: data
child1:  data

So apparently, when I call child1.method, the compiler directly set it to call
the child's method.  But, if I have parent* p = new child1; followed by a 
p->method call, then the program run time looks to the virtual function table.

Err, I think the virtual function table was set up at the object 
initialization, and that it looks up the function ID, and calls the pointer 
there.  So, when child1 is initialized, the entry in the table for method 
points to child1::method, but when child2 is initialized, the entry for method
points to parent::method.  But, this is what I got from reading between the 
lines.  I still haven't found a site that explicitly confirms or denies this.

> >In case people think I'm just pretending I know ways to encourage people to
> >help me with homework, I'll give you one way.
> 
> I don't mind helping with homework... we're a great resource to do 
> it.  Especially if you're stuck.

Well, at this point the homework has been submitted, and I really didn't need
this much detail to complete it.  I just thought it would be handy to be able
to say exactly what is happening the next time I feel like arguing with a 
professor about whether what he said made sense, or was a workable solution.
 
-- 
Joshua D. Boyd



More information about the geeks mailing list