[comp.lang.c++] Implementing inheritance

measures@evax.arl.utexas.edu (Mark Measures) (03/15/91)

I'm working in a group at UT Arlington and we are interested in how
C++ (g++, to be precise) implements inheritance.  We have the source
code, but before we go into a lot of code sniffing, I thought I'd ask
if anyone could give me a good description.

Thanks,
Mark Measures
measures@evax.uta.edu

gwu@nujoizey.tcs.com (George Wu) (03/16/91)

-
     I expect any good C++ book would contain a description of how C++
compilers implement inheritance.  Certainly, Stanley Lippman's "C++ Primer"
does so.

     In a sentence, a derived class is implemented as a structure
containing both derived and inherited data elements.  Accessing elements
defined in different parts of the inheritance tree is mostly a matter of
offsetting properly into the data structure.  There are, of course, lots of
other details, such as virtual functions and virtual inheritance.  For the
most part, add a level of dereference to implement them.

     Of course, most C++ books will not contain details specific to your
compiler.  If you really want that kind of information, I think you will
need to start reading the source code.

							George

----
George J Wu, Software Engineer        | gwu@tcs.com or uunet!tcs!gwu
Teknekron Communications Systems, Inc.| (415) 649-3752
2121 Allston Way, Berkeley, CA, 94704 | Quit reading news.  Get back to work.

rsalz@bbn.com (Rich Salz) (03/18/91)

In <1885@news.tcs.com> gwu@nujoizey.tcs.com (George Wu) writes:
>     I expect any good C++ book would contain a description of how C++
>compilers implement inheritance.
How many C books describe the compiler's implementation of typedef?

I wish I never had to learn anything about virtual function tables.  Unfortunately,
it seems impossible to learn C++ these days without knowing them.
-- 
Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.
Use a domain-based address or give alternate paths, or you may lose out.

steve@taumet.com (Stephen Clamage) (03/20/91)

rsalz@bbn.com (Rich Salz) writes:

>I wish I never had to learn anything about virtual function tables.  Unfortunately,
>it seems impossible to learn C++ these days without knowing them.

This seems like an odd comment.  Why would you need to know anything at
all about virtual tables to learn C++?  There is nothing at all in the 
language definition which requires virtual tables to exist, let alone
have any particular format.  It is a convenient implementation technique
used in current compilers, but you don't need to know about virtual tables
unless you are writing analysis tools which work on executable files.

You do need to understand the syntax and semantics of virtual functions,
along with the rules for scoping and name hiding, to use C++ effectively for
complex applications.  Knowing the details of virtual tables seems no
more necessary than knowing the details of any other code generation
technique.
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

al@well.sf.ca.us (Alfred Fontes) (03/20/91)

rsalz@bbn.com (Rich Salz) writes:

>I wish I never had to learn anything about virtual function tables.  Unfortunately,
>it seems impossible to learn C++ these days without knowing them.

Why?  Please explain.

bright@nazgul.UUCP (Walter Bright) (03/29/91)

In article <626@taumet.com> steve@taumet.com (Stephen Clamage) writes:
/Why would you need to know anything at
/all about virtual tables to learn C++?

Perhaps an example would help. I knew a guy in college once who was just
learning to program. He was very meticulous, and carefully read all the
pertinant manuals. Part of the program was used to write bytes to a file.
He modularized the function that did this, all to proper practice. It turns
out that the program ran incredibly slowly. He pulled his hair out trying
to figure out why, then at wit's end showed the program to his roommate.
His roommate found the problem immediately, it seems that in the function
to write the output to a file, in the inner loop he opened the file for
append, wrote a byte, and closed the file! Nothing anywhere in the manuals
said that this was the wrong way to write a file...

The point is, although theoretically you don't need to know how things are
implemented in order to use them, in the real world it is necessary to
understand what is going on. Not knowing how various C++ constructs are
implemented, and what the various runtime tradeoffs are, will inevitably
result in inefficient and large programs.

I recommend that professionals occaisonally look at the assembler output
of their compiler on their production code, the results can be very
enlightening! :-)

Disclaimer: I don't subscribe to the school of thought that program efficiency
is irrelevant. (It's only irrelevant for one-shot or rarely used programs.)