    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>ACCU  :: What's In a Struct?</title>
        <link>https://members.accu.org/index.php/articles/898</link>
        <description>Professionalism in Programming</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Administrator</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.xaraya.org" /> 
        <admin:errorReportsTo rdf:resource="mailto:webeditor@accu.org" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>




<div class="xar-mod-head"><span class="xar-mod-title">Programming Topics + CVu Journal Vol 11, #4 - Jun 1999</span></div>

<table border="0" cellpadding="1" cellspacing="0">
    <tbody>
    <tr>
        <td valign="top">
            Browse in :
       </td>
       <td valign="top">

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c13/">Topics</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c65/">Programming</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/">All</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c76/">Journals</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c77/">CVu</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c131/">114</a>
<br />

                                            <a href="https://members.accu.org/index.php/articles/c65-131/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c65+131/">All of these categories</a>
<br />
</td>
   </tr>
   </tbody>
</table>




<div class="xar-error">
   <p>
 <strong>Note:</strong> when you create a new publication type,
the articles module will automatically use the templates
<em>user-display-[publicationtype].xt</em>
and <em>user-summary-[publicationtype].xt</em>.
If those templates do not exist when you try to preview or display a new article,
you'll get this warning :-)  Please place your own templates in themes/<em>yourtheme</em>/modules/articles . The templates will get the extension .xt there. </p>
</div>
<div class="xar-norm xar-standard-box-padding">
   <h1><strong>Title:</strong>&nbsp;What's In a Struct?</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 03 June 1999 13:15:31 +01:00 or Thu, 03 June 1999 13:15:31 +01:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e18" id="d0e18"></a></h2>
</div>
<i><span class="remark">Note that the following article will
feature elements that expert readers may consider erroneous. Rather
than just ignore them, please do two things: discuss them with
Silas if possible and write an article to clarify the issue. One of
the toughest tasks for the expert is to get inside the thinking of
a very good student who has not yet grasped everything. Articles
like this one are therefore of particular value.</span></i>
<p>Recently I was trying to introduce object orientation to someone
familiar with an imperative programming language (namely Pascal),
and thought I would go via C <tt class="literal">struct</tt>s.
Unfortunately I only succeeded in confusing her, so now I'm writing
an article to confuse all the beginners in ACCU. Well, hopefully
not. This article does not go (far) into object-oriented design,
but it is my hope that it will leave the reader prepared to learn
about good object-oriented design without constantly puzzling over
how it relates to procedural programming.</p>
<p>Please note that, for brevity, several of the examples here
hard-code &quot;magic numbers&quot; into variable initialisations. In
practise this is a Bad Thing&trade; since it limits the capability
of the user to adjust the program to individual requirements. I
would not write example code that uses &quot;magic numbers&quot; anywhere
other than in variable initialisations, since this will also lead
to making the code difficult to maintain. For example, imagine
writing a long program and then realising that in lots of places
where you had said 17 you really meant 15. You can't just search
and replace 17 because there might be other 17s that don't need
changing, and in other places you might have wanted 17-1 and
written 16, and so on. It would take a very long time to put this
right and be reasonably sure you're not introducing any more bugs,
whereas if you had put it in a manifest constant you can change it
in seconds.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e29" id="d0e29"></a>structs</h2>
</div>
<p>What is a <tt class="literal">struct</tt>? You can think of it
as &quot;a way of grouping variables together For Some Strange Reason&quot;,
but it does help to know what is really going on. One important
thing about C is it's a compiled language, traditionally &quot;close&quot; to
machine code.</p>
<p>Experienced C programmers can write a few lines of C and know
exactly what assembler would be generated by them (at least, until
an optimiser comes along and thinks it knows better). But if it is
done well in C then it will be portable across different machines
(present and future) with different assembly codes. You can't
ignore the relationship between C and assembler, and in assembler
you need to know what's really going on in the computer's
memory.</p>
<p>Consider the following C:</p>
<pre class="programlisting">
int h;
struct {
  int a,b,c;
}p;
</pre>
<p>This effectively makes four integer variables: <tt class=
"varname">h</tt>, <tt class="varname">p.a</tt>, <tt class=
"varname">p.b</tt> and <tt class="varname">p.c</tt>. But how will
those variables be arranged in memory? There are two 'obvious'
arrangements:</p>
<p>Arrangement 1</p>
<div class="literallayout">
<p>Word 0: h<br>
Word 1: p.a<br>
Word 2: p.b<br>
Word 3: p.c</p>
</div>
<p>Arrangement 2</p>
<div class="literallayout">
<p>Word 0: p.c<br>
Word 1: p.b<br>
Word 2: p.a<br>
Word 3: h</p>
</div>
<p>Notice that <tt class="varname">p.a</tt>, <tt class=
"varname">p.b</tt> and <tt class="varname">p.c</tt> are guaranteed
to be in order, but there is no reason why <tt class=
"varname">h</tt> should be before them. If an optimising compiler
decides that, on a particular machine, it is best to swap
<tt class="varname">h</tt> and <tt class="varname">p</tt>, then it
is quite at liberty to do so. But it cannot change the order of
<tt class="varname">a</tt>, <tt class="varname">b</tt> and
<tt class="varname">c</tt> within <tt class="varname">p</tt>.</p>
<p>One reason why this is useful is that you can create a pointer
to <tt class="varname">p</tt> and pass it around your program. If
<tt class="varname">p</tt> were a very large structure then passing
a pointer around is very much cheaper than passing the whole thing
around. But all a pointer tells you is the memory address at which
<tt class="varname">p</tt> is stored. To read the values from it,
therefore, you need to know that the first value is <tt class=
"varname">a</tt>, the second is <tt class="varname">b</tt> and the
third is <tt class="varname">c</tt>. If some optimiser switched the
order behind your back, all kinds of things will go wrong. This is
why the order is guaranteed to be left alone (or at least, it's
guaranteed that your program will behave as if the order is left
alone).</p>
<p>[<i><span class="remark">By the way, I do not have access to an
authoritative copy of the standard, so if anybody spots something
wrong then please write in.</span></i>]</p>
<p>Passing pointers really comes into its own when you want your
functions to modify the values in the original <tt class=
"literal">struct</tt>. Without pointers and <tt class=
"literal">struct</tt>s, the only way you can do this is to have
global variables, which are bad things&trade;. To illustrate, I've
written a music program that needs to store a lot of information
about the piece as it goes. In the early days (years ago), when I
wanted another variable, I would very often think &quot;oh let's just
have a global - I want to get this done quickly and nobody's going
to notice&quot;. Then one day I thought, &quot;wouldn't it be nice to edit
several pieces of music at once?&quot; And I realised how many globals I
had accumulated, all scattered throughout the modules - every
single one of them must go before the program can cope with
multiple independent scores. I made a token effort but it got so
tedious that I never actually finished. My shortcuts had caught up
with me.</p>
<p>A simple-minded solution to the above problem (if you can stand
going through reams of messy old code) is to put all of the globals
into a <tt class="literal">struct</tt>, and pass a pointer to that
<tt class="literal">struct</tt> around the program. What I was
actually trying to do was more complicated, but it has the same
basic idea. Then, if you wanted several pieces of music, you can
just create an instance of the <tt class="literal">struct</tt> for
each piece, and pass the appropriate one to the program:</p>
<pre class="programlisting">
int main(){
  struct {
      /* hundreds of variables go here */
  } first_piece,second_piece;
      /* initialise them here */
  draw_music(first_piece);
  draw_music(second_piece);
}
</pre>
<p>Actually there are several things wrong with the above. The most
obvious one is the fact that the &quot;hundreds of variables go here&quot;
<tt class="literal">struct</tt> is within <tt class=
"function">main()</tt>. As it is, you would need to list all of
those variables again every time the <tt class=
"literal">struct</tt> is used. This is tedious and prone to error,
not to mention how awkward it would be to add a variable later. But
doing boring repetitive things is precisely what computers are good
at, and needless to say there is a way of automating it. Actually
there are several, and the two most common ones are:</p>
<p>Method 1:</p>
<pre class="programlisting">
typedef struct {
      /* hundreds of variables go here */
} Piece_of_music;
int main() {
  Piece_of_music first_piece,second_piece;
  draw_music(first_piece); draw_music(second_piece);
}
</pre>
<p>Method 2:</p>
<pre class="programlisting">
    struct Piece_of_music {
  /* hundreds of variables go here */
};
int main() etc
</pre>
<p>Method 1 is easy to understand when you think about it: What the
'<tt class="literal">typedef</tt>' effectively does is to replace
'<tt class="type">Piece_of_music</tt>', whenever it occurs where a
type of variable should go, with the <tt class=
"literal">struct</tt>. I think method 2 is neater but I know some
readers will disagree. With method 2, you can also create instances
of the <tt class="literal">struct</tt> between the closing brace
and the semicolon if you want.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e176" id="d0e176"></a>Header Files
and Linking</h2>
</div>
<p>With either method, what you would actually do (if you had any
sense) would be to put the <tt class="literal">struct</tt> part in
a header file. I expect most readers know about header files but
since I recently came across someone who was writing lengthy
programs without them (and there are some very poor books out
there), I think I'd better re-iterate. Consider the following
C:</p>
<pre class="programlisting">
int main() {
  puts(&quot;Goodbye cruel world&quot;);
}
</pre>
<p>This is valid C and will compile and run. But <tt class=
"function">puts</tt> is a function that is in the standard library,
not in your file. The compiler will not look at the standard
library unless you tell it to. So how does it know what machine
code to generate for that function call? How can it create the jump
to the place in memory where the code for <tt class=
"function">puts</tt> is stored, if it doesn't know what <tt class=
"function">puts</tt> is? It also needs to know what sort of
parameters it takes and what type of value it returns, in order to
generate the necessary machine code. And yet you haven't told it
any of these things. What's going on?</p>
<p>Things get stranger. If you tried compiling the above with a C++
compiler, you would get something like &quot;Error at line 2: implicit
declaration of function puts&quot;. Yet, a C compiler won't complain at
all. This is one of the subtle differences between C and C++ that
people sometimes don't know.</p>
<p>Generating an executable has two main stages: compiling and
linking. This simple thing is missed out of some introductions
because most compilers these days automatically run the linker when
they have finished. I'm fairly convinced that it's not a good idea
to leave out details for beginners just because they're hidden. It
only results in the beginners not really understanding what's going
on. I once saw someone who seemed to be determined to teach Java
without mentioning pointers, because although pointers are
everywhere in Java they are hidden, so why bother the newcomers
with them? Needless to say, the sample of people I talked to (who
hadn't met Java before) couldn't explain what <tt class=
"literal">new</tt> did, and many of them didn't understand the
difference between a <tt class="literal">class</tt> and an instance
and what <tt class="literal">static</tt> meant. Then one day
someone raised his hand and asked, &quot;What does it mean <tt class=
"exceptionname">NullPointerException</tt>?&quot; &quot;Gotcha&quot;, I thought.
But no... And missing out the mention of the linker in C is almost
as bad as missing out pointers in Java. (Incidentally, Java does
not have a linker because the classes are loaded in as needed by
the program, which has its advantages and disadvantages.)</p>
<p>When the compiler compiles your program, it does so one module
at a time. By &quot;module&quot; I mean file - if you have thousands or even
tens of thousands of lines in your program, putting it all in one
file can make it difficult to manage. Because most compilers only
look at one module at once, you can compile a library of functions
for use in somebody else's program, and this is what the standard
libraries are, including <tt class="function">puts()</tt>. What the
linker does is to take the output of the compiler (the object file)
for each of your modules, and the standard libraries (usually), and
'link' them together. So if in one of your modules you call a
function that is defined in another, the compiler will generate the
machine code to call the function, but will leave the actual memory
address (telling the processor where the machine code for the
function actually is) blank. The linker will find that function and
fill in the actual address, and make sure it's included in the
final program (if it's a sensible linker then it will only include
the functions that you actually use, so not every standard library
function gets into your executable).</p>
<p>The compiler does not need to know where <tt class=
"function">puts</tt> actually is, because the linker will fill in
that part, but it does need to know what types of variable
<tt class="function">puts()</tt> takes as parameters and what type
it returns. This is so your program can be checked and the required
machine code generated. In C, if you don't tell the compiler these
things, it will guess. The people making the C++ standard realised
that, if you leave the compiler to guess things, it's easier to
make a mistake without realising it, so in C++ you MUST declare
your functions explicitly. (<i><span class="remark">Actually C++
uses function overloading and that effectively requires visible
function declaration at the point of compilation.
Francis</span></i>). This is why the above code works in C but not
in C++ (if the compiler's any good). C will guess by looking at the
types of variable you actually give the function (it assumes you
are right) and assuming it returns an <tt class="type">int</tt>. In
this case it is correct, but that is not always the case and is
dangerous to rely on.</p>
<p>How, then, do you tell the compiler? Well, you could do it
yourself by adding the following:</p>
<pre class="programlisting">
int puts(char const * string_to_put);
</pre>
<p>but let me say immediately that this is not the best way of
doing it (<i><span class="remark">and is actually a mistake in C++
where you must not provide your own declarations of library
functions. Francis</span></i>). What it says is, there is a
function called <tt class="function">puts</tt> that returns an
<tt class="type">int</tt> and takes a '<tt class="type">char
const*</tt>' (don't worry about that yet) as a parameter (the
's<i class="parameter"><tt>tring_to_put</tt></i>' in this case can
be left out, but it does make the program more legible). When you
are actually writing the function, you put braces rather than the
semicolon and can then refer to the parameter <i class=
"parameter"><tt>string_to_put</tt></i> as a variable.</p>
<p>Aside: Originally in C you had to write functions like this:</p>
<pre class="programlisting">
int my_func(my_parameter)
long my_parameter;
  {
  /* code goes here */
  }
</pre>
<p>but these days most people agree that that's ugly and outdated
(<i><span class="remark">and won't work in C++.
Francis</span></i>). As for where to put the '<tt class=
"literal">int puts(char const* string_to_put);</tt>', at the top of
the program (before <tt class="function">main()</tt>) is a good
idea because then it is visible throughout the file; if you put it
within <tt class="function">main()</tt> then it will only be
visible within <tt class="function">main()</tt>, and if you put it
at the very end then the compiler won't know about it the first
time it reads <tt class="function">main()</tt>. Compilers are very
fussy things - they like to know about everything in advance and
usually can't look further down in the file for them. So even if
you did put all your functions in one file, you may still have to
declare them before you write them.</p>
<p>With standard libraries, it's not a good idea to declare all the
functions yourself because you might make a mistake. Indeed, even
with your own functions, it would be nice if you could declare them
only once and use that declaration for all the files in your
program that need it. This saves you from having to make lots of
changes (some of which you might forget) if you want to modify it.
Again, we have repetition, so programmers inevitably invented an
automatic way of avoiding it (a general rule is: if you ever find
yourself doing something repetitive with computers, something is
wrong). This method is header files.</p>
<p>The principle behind a header file is very simple: You tell the
pre-processor (which is something that runs just before the
compiler does) to include another file into this one. So, for
example, you can have two files, <tt class="filename">file1.h</tt>
and <tt class="filename">file2.c</tt> (header files by convention
have an extension <tt class="filename">.h</tt>):</p>
<p>file1.h:</p>
<pre class="programlisting">
typedef struct {
   /* hundreds of variables go here */
} Piece_of_music;
void draw_music(Piece_of_music p);
</pre>
<p>file2.c:</p>
<pre class="programlisting">
#include &quot;file1.h&quot;
int main() { etc
</pre>
<p>and what happens should be obvious in the light of the previous
example. By convention, you do not compile the <tt class=
"filename">.h</tt> files (they are automatically included in the
files that you do compile), and you can give each <tt class=
"filename">.c</tt> (or <tt class="filename">.cpp</tt>) file a
corresponding <tt class="filename">.h</tt> file, all with
meaningful names. Thus <tt class="filename">file1.h</tt> might be
better called <tt class="filename">piece_of_music.h</tt>, and in
all the files in your program where that struct is needed you can
start with <tt class="literal">#include &quot;piece_of_music.h&quot;</tt>.
Then, if you need to change something, you need do it only
once.</p>
<p>The standard functions like <tt class="function">puts</tt> are
in a file called <tt class="filename">stdio.h</tt> that comes with
the compiler (stdio is short for &quot;STandarD Input and Output&quot;
library), and instead of writing</p>
<pre class="programlisting">
#include &quot;stdio.h&quot;
</pre>
<p>you write</p>
<pre class="programlisting">
#include &lt;stdio.h&gt;
</pre>
<p>which makes sure that the compiler's directory is checked for
the file rather than the current directory (where your own files
are). (<i><span class="remark">It is a bit subtler than that
because the header files for the Standard Library don't have to
exist, the compiler can know what is in them and use that knowledge
direct as long as you have written the correct #include.
Francis</span></i>) (Some time ago I spent ages trying to work out
why my program wouldn't compile on a shared system, and it turned
out that some joker had changed the compiler's header files - this
is an evil thing to do.)</p>
<p>One other thing about header files is you can have nested
includes, so you can have one header file including another. So, if
you're not careful, you can have two files trying to include each
other, which is not a good idea. To get around this possibility
(and also because including a header twice by accident can
generally confuse things) you can write all your header files like
this:</p>
<pre class="programlisting">
#ifndef PIECE_OF_MUSIC_H
#define PIECE_OF_MUSIC_H
typedef struct {
  /* hundreds of variables go here */
} Piece_of_music;
#endif
</pre>
<p>Any line beginning # gets sent to the pre-processor (like
<tt class="literal">#include</tt> does). The first line means &quot;only
pay attention to the following code if the pre-processor macro
<tt class="literal">PIECE_OF_MUSIC_H</tt> is not defined&quot;
(<tt class="literal">ifndef</tt> is short for &quot;if not defined&quot;;
there is also a <tt class="literal">#ifdef</tt>). I'd better not go
into all the complications of pre-processor macros here, but in the
simplest form they can be either defined or not, and many
programmers put them in all capital letters by convention. To begin
with, <tt class="literal">PIECE_OF_MUSIC_H</tt> is not defined, so
the <tt class="literal">#ifndef</tt> and the <tt class=
"literal">#endif</tt> are ignored. However, <tt class=
"literal">#define PIECE_OF_MUSIC_H</tt> causes it to become
defined. So if the header file were included twice in the same
module, the second time it is included the pre-processor will
ignore everything between the <tt class="literal">#ifndef</tt> and
the <tt class="literal">#endif</tt>, which amounts to not including
the file. Similarly, if two files tried to include each other, then
the first could include the second, but the pre-processor won't let
the second include the first because by that time the first would
have defined a macro that prevents it from being included
again.</p>
<p>There is something else that is worth noting about the linker,
but is also left out in many introductions. This is &quot;name
mangling&quot;, and is what causes those bizarre error messages that you
get when you mismatch your types between modules. The compiler can
check the types within a module (e.g. if you said that a function
takes an integer parameter then it can complain if you try to give
it a pointer), but it cannot check the types between modules. That
is left to the linker. And the way it is done is to modify the
names of the variables so as to include type information, so if
there is a type mismatch across modules then the names will not
match up. This &quot;name mangling&quot; can mean that messages from the
linker are highly cryptic, but &quot;undefined symbol&quot; usually means a
type mismatch across modules. (<i><span class="remark">Note that a
C compiler should not do this, it is a C++ mechanism.
Francis</span></i>)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e384" id="d0e384"></a>Pointers and
Stuff</h2>
</div>
<p>As it stands, <tt class="function">main()</tt> passes the
<tt class="type">Piece_of_music</tt> structure to <tt class=
"function">draw_music</tt> by value, which means it makes a copy.
With large structures this can be expensive in processing time and
memory requirements, and it may be better to pass a pointer (or in
C++ a reference). But several things need to be considered. Is the
function allowed to change the thing that is pointed to? It can be
useful, but it might be undesirable. If not then it should be
marked as <tt class="literal">const</tt>, so the compiler can check
to make sure that you really don't change it. But it is possible to
override this, so whenever you pass a pointer to a library function
written by somebody else, you are trusting them not to change the
thing it's pointing at if they say they won't. In most cases you
should be all right - if somebody is being particularly malicious
in a library then that library (or compiler) should not get good
reviews.</p>
<p>But there are still traps to fall into. One is when a function
takes two pointers, one as a <tt class="literal">const</tt> and the
other as something it's supposed to change, but actually they point
to the same thing. For example, consider the following:</p>
<pre class="programlisting">
strcat(a,a);
</pre>
<p><tt class="function">strcat</tt> is a standard string function
(provided by <tt class="literal">#include &lt;string.h&gt;</tt>)
that concatenates one string onto another. A string in C is an
array of characters, and you can pass arrays around cheaply by
giving a pointer to the first element (a zero byte is stored at the
end of the string to terminate it). So the library functions often
take arguments of type <tt class="type">char*</tt> (i.e. pointer to
char) and <tt class="type">char const *</tt> (pointer to char but
you've promised not to change the thing it's pointing at).
<tt class="function">strcat</tt> takes parameters of <tt class=
"type">char*</tt> and <tt class="type">char const*</tt>, for the
destination and source strings respectively. To see what is wrong
with the above, consider the following example implementation of
strcat:</p>
<pre class="programlisting">
char* strcat(char* destination,char const* source) {
  int d,i;
  d=0;
  while(*(destination+d)!=0) {
    d=d+1;
  }
  i=0;
  while(*(source+i)!=0) {
    *(destination+d)=*(source+i);
    d=d+1;
    i=i+1;
  }
  *(destination+d)=0;
  return destination;
}
</pre>
<p>(Yes I know I could have written it much more briefly, but I'm
trying to make clear what's going on.)</p>
<p>Remember <i class="parameter"><tt>destination</tt></i> is a
pointer, and the <tt class="literal">*</tt> on the front is a
dereferencing operator, meaning &quot;go and get the value stored at
this pointer&quot;. So to begin with the value at <i class=
"parameter"><tt>destination</tt></i> is got, and compared with 0 to
see if it's the end of the string. If not, the value at <tt class=
"literal">destination+1</tt> is likewise, then <tt class=
"literal">destination+2</tt> and so on. When the first <tt class=
"literal">while</tt>-loop finishes, the variable '<tt class=
"varname">d</tt>' holds the length of the string, and writes to the
address (<tt class="literal">destination+d</tt>) can append
characters to the string. This is what the second <tt class=
"literal">while</tt>-loop does, using 'i' to keep track of which
character in the source is being written. Finally, an extra 0 byte
is written to <i class="parameter"><tt>destination</tt></i> to mark
the end of the string, because the condition in while is tested
before the code executes, so the 0 would not have been copied.</p>
<p>(The fact that the function returns <i class=
"parameter"><tt>destination</tt></i>, rather than <tt class=
"type">void</tt>, enables you to write code like <tt class=
"literal">strcat(strcat(a,b),c);</tt>. C is full of things like
that, e.g. you can say <tt class="literal">a=b=c=5;</tt> because
the operator '=' returns its left-hand operand.)</p>
<p>Going back to our example, if this function were called with
both parameters pointing to the same thing, it would loop forever
and overwrite the whole address space, as you can convince yourself
by following the code through. In general, if there is any danger
of your functions getting two or more pointers to the same thing,
one of which can be changed, then you need to be very careful.</p>
<p>Of course, I was really making the point for your own functions
rather than the standard libraries, but it is generally a good idea
to avoid risking it anyway. The standard libraries could of course
be implemented in such a way that it didn't matter (in the above
example, the length of the source string could be found before any
of the copying is done), but this might not always be the case.
Interestingly, the <tt class="function">strcat</tt> that comes with
<span class="application">gcc</span>, when given the following
program:</p>
<pre class="programlisting">
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
int main() {
  char test[100];
  strcpy(test,&quot;allo &quot;);
  strcat(test,test);
  puts(test);
}
</pre>
<p>produced &quot;allo allo a&quot;.</p>
<p>To modify the music example to pass by pointer, you need to
change the declaration of <tt class="function">draw_music</tt> to
take a parameter of type <tt class="type">Piece_of_music *</tt> (it
doesn't matter whether or not there's a space before the <tt class=
"literal">*</tt>) or <tt class="type">const Piece_of_music *</tt>,
instead of <tt class="type">Piece_of_music</tt>. Within the body of
that function, you need to dereference the pointer before accessing
its members. So if one of the fields of <tt class=
"type">Piece_of_music</tt> were called <tt class=
"varname">number_of_bars</tt>, the following two fragments of code
are OK:</p>
<pre class="programlisting">
void draw_music_1(Piece_of_music p) {
    p.number_of_bars=3;
}
void draw_music_2(Piece_of_music* p) {
    (*p).number_of_bars=3;
}
</pre>
<p>but the following is not:</p>
<pre class="programlisting">
void draw_music_wrong(Piece_of_music* p) {
  p.number_of_bars=3;
}
</pre>
<p>because '<i class="parameter"><tt>p</tt></i>' is simply a
pointer, and pointers do not have fields called <tt class=
"varname">number_of_bars</tt>. Also note the brackets around
<i class="parameter"><tt>*p</tt></i>, since the dot has a higher
precedence than the indirection operator, so without the brackets
it would get interpreted as <tt class=
"literal">*(p.number_of_bars)</tt> which is wrong.</p>
<p>An abbreviation for <tt class="literal">(*p).number_of_bars</tt>
is <tt class="literal">p-&gt;number_of_bars</tt> (read &quot;p arrow
number_of_bars&quot;), and this is usually preferred. In Java,
everything is done as if with the arrow operator, but just to be
confusing it is written as a dot.</p>
<p>The other thing that needs to be changed is in <tt class=
"function">main</tt>, where <tt class="varname">first_piece</tt>
and <tt class="varname">second_piece</tt> are passed by value. To
pass the memory address, you need to prefix them with an ampersand
(<tt class="literal">&amp;</tt>) (a &quot;take the address&quot; operator),
i.e.</p>
<pre class="programlisting">
draw_music(&amp;first_piece);
draw_music(&amp;second_piece);
</pre>
<p>In C++ there is a nice thing called &quot;references&quot; that means you
don't have to remember all of these differences. In other words,
you can continue to use the dot operator and you don't worry about
the ampersands in <tt class="function">main</tt>. All you have to
do is put an ampersand where <tt class="function">draw_music</tt>
is declared (and where it is written),</p>
<pre class="programlisting">
void draw_music(Piece_of_music &amp;p);
</pre>
<p>and the compiler does the rest for you - you can pretend that
you're passing by value but actually you're passing by pointer (but
this time it's called reference) (<i><span class="remark">Well that
cuts a few corners. Francis</span></i>). There is much less
confusion to be had from references than from pointers and
references tend to produce cleaner-looking code, although the traps
like what happens if you pass two references to the same thing, one
of which can be changed, are still there, even in Java, subtly
hidden away, waiting to prey on the innocent...(<i><span class=
"remark">Actually it is worse, because instances of user defined
types in Java are anonymous. But let us not get into that here.
Francis</span></i>)</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e579" id="d0e579"></a>Other
Stuff</h2>
</div>
<p>A <tt class="literal">struct</tt> is a type in the same way as
the primitive types (<tt class="type">int</tt>, <tt class=
"type">char</tt>, <tt class="type">long</tt> etc) are types, and
you can use them in very similar ways. If you have defined a
<tt class="literal">struct</tt> then you have defined your very own
type of variable. You can have arrays of that variable:</p>
<pre class="programlisting">
Piece_of_music haydn_symphonies[104];
strcpy(haydn_symphonies[ 99].title,&quot;The Clock&quot;);
strcpy(haydn_symphonies[103].title,&quot;The London&quot;);
</pre>
<p>(note that offsets range from 0 through 103 not from 1 through
104, hence symphony 100 is at offset 99; also I have assumed that a
string called 'title' with sufficient reserved space is one of the
&quot;hundreds of variables&quot;). You can have dynamically allocated
<tt class="literal">struct</tt>s (especially in C++ with <tt class=
"literal">new</tt> and <tt class="literal">delete</tt>, but I'd
better not go into those now). In C++ you can say that a function
is not allowed to change a <tt class="literal">struct</tt> even
when it is passed by value:</p>
<pre class="programlisting">
void my_function(My_struct  const  do_not_touch) {
  do_not_touch.a=4;  // This will cause an error
}
</pre>
<p>(<i><span class="remark">That should be true in C as well.
Francis</span></i>)</p>
<p>You can also write <tt class="literal">struct</tt>s out to a
file and read them in quite easily. There are a few pitfalls if you
expect the resulting file to be readable when your program is
compiled with a different compiler. (in particular, some compilers
'pad out' your <tt class="literal">struct</tt>s with unused bytes
because some processors can deal with them quicker if their size is
a multiple of 2 or 4 or 8). Generally speaking it's best to write
your own saving and loading code (in Java you can use serialisation
to do it for you). If you want to know the size that your
<tt class="literal">struct</tt> takes up, as with any other
variable type you can use the <tt class="function">sizeof()</tt>
operator, so <tt class="literal">sizeof(Piece_of_music)</tt> will
evaluate as the number of bytes required to store all the variables
that are in a <tt class="type">Piece_of_music</tt>.</p>
<p>You can have <tt class="literal">struct</tt>s within <tt class=
"literal">struct</tt>s: (<i><span class="remark">But careful
because the rules are different for C and C++.
Francis</span></i>)</p>
<pre class="programlisting">
main() {
  struct French_Suite {
    Piece_of_music prelude,sarabande,minuet,courante,rondeau;
  } my_piece;
  my_piece.sarabande.number_of_bars=80;
    /* other initialisation stuff here */
  draw_music(my_piece.minuet);
}
</pre>
<p>and you can have <tt class="literal">struct</tt>s that contain
pointers to other <tt class="literal">struct</tt>s of the same
type:</p>
<pre class="programlisting">
struct Linked_list {
  Piece_of_music data;
  Linked_list next;
};
</pre>
<p>You can also (although you rarely need to) have a pointer to a
<tt class="literal">struct</tt> before you have told the compiler
what is actually in the <tt class="literal">struct</tt>:</p>
<pre class="programlisting">
struct second_struct;
struct first_struct {
  int a;
  second_struct* pointer;
};
struct second_struct {
  double b;
  first_struct* pointer;
};
</pre>
<p><tt class="literal">struct</tt>s really come into their own when
you are implementing data structures, such as linked lists, binary
trees, hash tables, stacks, queues, and so on, during which you may
have an arbitrary number of some item in memory (you don't know in
advance how many there will be), memory can be allocated and
deallocated as your program is running, and it is helpful (more
often than not, essential) to package things together and point to
them.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e679" id="d0e679"></a>Towards
Object Orientation</h2>
</div>
<p>Object orientation strongly features the &quot;least privilege
principle&quot;, which states that, in order to detect design errors,
you should ensure that each part of the program can only have the
privileges that it needs for its job. It is as though your program
is a top-secret operation being conducted on a &quot;need to know&quot; basis
- if some part of the program doesn't need to know something, then
it doesn't. This does not just mean restricting the variables it
can write to, but also restricting those that it can read, and the
information it can get. If you write code that tries to do
something that you said should not be done, the compiler can detect
this and tell you, which is much better than it going unnoticed and
you having to track down the bug later. The least privilege
principle does not just apply to variables, but this is where it is
most often found.</p>
<p>In fact, if you use the least privilege principle, your code
will not only be safer but may also be faster and/or smaller. This
is because a good compiler can sometimes use the restrictions you
have placed on your code to get a better idea of what you are up
to, and it might know a good optimisation for it. So even if you
think you are perfect, there is good reason to use the least
privilege principle.</p>
<p>A powerful feature of C++ in this respect is its <tt class=
"literal">public</tt>, <tt class="literal">private</tt> and
<tt class="literal">protected</tt> access modifiers. But before
they make sense, you need to know about 'methods'. These are
basically functions inside <tt class="literal">struct</tt>s. For
example, consider:</p>
<pre class="programlisting">
struct Picture {
  int x;  /* etc */
};
void draw_picture_to_screen(Picture &amp;p) {
  do_something_with(p.x);
}
int main() {
  Picture myPicture;  myPicture.x=3;
  draw_picture_to_screen(myPicture);
}
</pre>
<p>C++ will let you re-write this as follows:</p>
<pre class="programlisting">
struct Picture {
  int x;  // etc
  void draw_to_screen();
};
void Picture::draw_to_screen() {
  do_something_with(x);
}
int main() {
  Picture myPicture;
  myPicture.x=3;
  myPicture.draw_to_screen();
}
</pre>
<p>As before, you would replace the dot with an arrow if <tt class=
"varname">myPicture</tt> were a pointer. The code is already
looking cleaner, but the real advantage of methods becomes apparent
when you use the modifiers.</p>
<p>What is happening in the latter example is <tt class=
"function">Picture::draw_to_screen</tt> is implemented as a
function, as before, with a hidden pointer to the structure that it
is working with. This pointer is called this (for &quot;this object&quot;)
and you can access it yourself if you like: you can call <tt class=
"literal">do_something_with(this-&gt;x)</tt> just as easily as
<tt class="literal">do_something_with(x)</tt>. However, doing this
is only really useful when there is another <tt class=
"varname">x</tt> you need to worry about, and if this is the case
then your naming convention is probably too confusing. So in
practise you don't really need to know about <tt class=
"literal">this</tt> except when you need to tell some other object
about where in memory this object is (and you'll find out when
that's useful when you look at object-oriented design). It is
important to remember the difference between a <tt class=
"literal">struct</tt> and an instance of a <tt class=
"literal">struct</tt>. <tt class="type">Picture</tt> defines a
general type of variable, and the picture that is being acted on is
a particular instance of that; there could be others. It is the
same idea as the one behind the <tt class=
"type">Piece_of_music</tt> example.</p>
<p>To illustrate access modifiers, let's modify the <tt class=
"type">Picture</tt> <tt class="literal">struct</tt> a little:</p>
<pre class="programlisting">
struct Picture {
private:
  int x;  // etc
public:
  void draw_to_screen() const;
  void set_x_to(int new_x) { x=new_x; }
};
void Picture::draw_to_screen() const {
  do_something_with(x);
}
int main() {
  Picture myPicture;  myPicture.set_x_to(3);
  myPicture.draw_to_screen();
}
</pre>
<p>I have introduced three things here: Access modifiers, an inline
method and (for good measure) a <tt class="literal">const</tt>
method. The latter is indicated by the word <tt class=
"literal">const</tt> after the <tt class=
"methodname">draw_to_screen()</tt>, and it means that <tt class=
"methodname">draw_to_screen</tt> is not allowed to change any of
the member variables of the picture. This is an example of the
least privilege principle, so any methods that CAN be <tt class=
"literal">const</tt> SHOULD be (except constructors and destructors
and certain cases in inheritance, but you're not supposed to know
about that yet...). By default, everything in a <tt class=
"literal">struct</tt> is <tt class="literal">public</tt>, which
means that anybody anywhere in the program can access them (if they
can access an instance of the <tt class="literal">struct</tt>
itself). Things that are <tt class="literal">private</tt> can only
be accessed by a method of the <tt class="literal">struct</tt>.
That is why the <tt class="methodname">set_x_to()</tt> method is
required, since otherwise there is no way of getting to x from
outside the program. So, as it stands, we have allowed anybody to
set <tt class="varname">x</tt>, but nobody can read it (and after
all, nobody needs to read it so why allow them to?) But we can do
better than that: We can make sure that <tt class="varname">x</tt>
is set only once and cannot be set again (unless you use a nasty
cast), by giving the <tt class="literal">struct</tt> a
constructor:</p>
<pre class="programlisting">
struct Picture {
private:
  const int x;  // etc
public:
  Picture(int in_x);
  void draw_to_screen() const;
};
Picture::Picture(int in_x) : x(in_x) {
  // This code is executed when you make a Picture
  // Nothing here in this example
}
int main() {
  Picture myPicture(3);
  myPicture.draw_to_screen();
}
</pre>
<p>Now <tt class="varname">x</tt> is a <tt class=
"literal">const</tt>, i.e. it cannot be modified, even by a picture
itself. It can only be set in the 'constructor', a method that is
called when a new instance of the <tt class="literal">struct</tt>
is created (notice how the parameter is passed from <tt class=
"methodname">main</tt>). All <tt class="literal">struct</tt>s have
constructors, but if you don't supply one yourself then the
compiler will make one for you (that doesn't do much). They also
have destructors, which are methods that are called just before an
instance of the <tt class="literal">struct</tt> is deleted from
memory (either explicitly by the programmer or because it was a
local variable and the function or whatever is now about to
return). Destructors are useful for &quot;cleaning up&quot;, e.g. freeing any
resources associated with that object, deleting other objects it
has made for itself, and so on. Their use becomes clear as you do
object orientation. As the constructor for <tt class=
"classname">Picture</tt> is <tt class="methodname">Picture</tt>,
the destructor is <tt class="methodname">~Picture</tt>.</p>
<p>C++ has another keyword, <tt class="literal">class</tt>, that is
used in a similar way to <tt class="literal">struct</tt>. The
difference between C++ <tt class="literal">struct</tt>s and
<tt class="literal">class</tt>es is that members of <tt class=
"literal">class</tt>es are <tt class="literal">private</tt> by
default (until you put a modifier) whereas members of <tt class=
"literal">struct</tt>s are <tt class="literal">public</tt> by
default. For this reason, <tt class="literal">class</tt> is
preferred because it is safer. In practise this does not make a lot
of difference, because a common convention places a <tt class=
"literal">class</tt>' <tt class="literal">public</tt> methods at
the top (so they are among the first things you see when you view
the file). Often the first thing you do when you write a class is
along the lines of:</p>
<pre class="programlisting">
class Picture {
public:
  Picture();
  ~Picture();
};
</pre>
<p>Note that constructors and destructors had usually better be
<tt class="literal">public</tt>, but there are some exceptions to
do with inheritance and constructing via static methods (you'll
meet those later). I have been using <tt class=
"literal">struct</tt>s rather than classes in this article mainly
so that I could delay the introduction of access modifiers until I
had explained what was going on. Normally, though, C++ programmers
use classes. There is nothing to stop you from writing a C++
program with <tt class="literal">struct</tt>s, but it is not &quot;the
done thing&quot;. A good convention is that classes have NO PUBLIC
VARIABLES at all; all member access must be done through methods.
Even if you think that some member data should be public, you may
change your mind (or your design) later, and if you have
implemented it using 'get' and 'set' methods already then you do
not have to go through your entire program putting them in. 'get'
and 'set' methods also allow you to intercept these actions and do
useful work (e.g. you may want to log them, or mark something as
needing recalculation when its variables are changed, or whatever)
(<i><span class="remark">However a lot of get and set methods
usually suggests that you have yet to fully understand
object-orientation. Francis</span></i>). Don't worry about speed:
If you use inline methods for 'get' and 'set' (like the inline
'set' in the above example) then the compiler will generate exactly
the same machine code as it would if you access the variables
directly.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e876" id="d0e876"></a>Sneak Preview
of Further OOP Features</h2>
</div>
<p>I have to end this article somewhere, but just to tempt you,
here is a brief mention of some of the other things that you can do
in object-oriented programming. Container classes and templates:
There are all kinds of data structures that you can put your data
in, from the humble array and linked list, through binary trees and
hash tables, to very specialist structures that allow fast storage
and retrieval of a particular type of data. If you were writing a
linked list in a conventional language then (unless you did a nasty
trick) you could easily end up re-writing a linked list for every
type of variable that you want to link, in every program that you
want to do it. But if you wrote a linked list container class using
something called 'templates', it will be a Linked List To End All
Linked Lists, the last one you ever have to write (if done well).
Whenever you need a linked list of anything, one line will tailor
the very code you need from your template. And the same goes for
the more powerful data structures, such as hash tables (which can
be very efficient dictionaries).</p>
<p>Needless to say, other people have already written all those
nice container classes, and one effort was so good that it got
included in the C++ standard - it's called the Standard Template
Library (STL). So while writing linked lists and other stuff can be
instructive, these days it's really an academic exercise because
you can use the STL classes very simply. In effect, you just say
&quot;I'd like a hash table of Pictures please&quot; and it's yours. This
means that some programs that would otherwise use rough-and-ready
hacked-out data structures for the sake of getting the program
working quickly (and the programmer can't really be bothered that
the data is stored in the best way possible) can now use
top-quality ones even more quickly (if you're familiar with the
STL). And, of course, if someone in the future invented a brilliant
new data structure that you can't wait to use in your program, and
implemented a container class for it, you would not have to do much
work at all to change your program to use it.</p>
<p>Inheritance and virtual methods: The usefulness of this is best
understood by example. Suppose you have a function or method that
can take as a parameter something of type <tt class=
"type">GraphicsDevice*</tt> (for example), and call its methods,
such as line(), circle() and so on. But we haven't said what sort
of graphics device it is - it might be a screen, a printer, a
window, an image file on the disk, or even something like a turtle.
All of these will have different ways of drawing lines and circles,
so they will need different <tt class="methodname">line()</tt> and
<tt class="methodname">circle()</tt> methods. Inheritance lets you
say things like &quot;All graphics devices can draw lines&quot; and &quot;A screen
is a graphics device, and this is how it draws its lines&quot;. The
method that is actually doing the drawing doesn't need to know what
it is drawing to, and this makes writing the program much simpler.
And then if I came to you and said something like &quot;Please can you
get it to output the graphs to a Braille embosser?&quot; and gave you
some methods to do it, you would only need to change one or two
parts of your program, rather than having to go through the whole
lot. Also, if you compiled your program on a different operating
system with a different way of drawing things, you only need
re-write a small part of your graphics library, rather than the
whole program.</p>
<p>Inheritance can have multiple layers. For example, the graphics
library in my music program has a subclass of <tt class=
"classname">GraphicsDevice</tt> called <tt class=
"classname">BitmapDevice</tt> (I think, he says without having the
source in front of him), and there is code in <tt class=
"classname">BitmapDevice</tt> for drawing lines, circles etc to a
bitmap in memory. Then the bitmapped image files and the dot matrix
printers can all inherit from <tt class=
"classname">BitmapDevice</tt> rather than <tt class=
"classname">GraphicsDevice</tt>, since they all share the bitmap
code and only differ in the way that they output the final
bitmap.</p>
<p>Other devices, like PostScript printers, can inherit directly
from <tt class="classname">GraphicsDevice</tt>. This gives you an
idea that inheritance is a useful thing, but you can make some much
more complicated structures than I have described (you can have
stuff like classes inheriting from more than one class at once,
which Java does using interfaces instead). However, views do differ
on this and many say that, if you make your inheritance structures
too complicated, you will only make things confusing. That is why
Java replaced multiple inheritance with a simplified version called
interfaces. As with anything in design, good design uses the
features you need when you need them, rather than using every
possible feature just to show off. Exception handling: Imagine in a
conventional language you call a function, which calls a function,
which calls a function, and so on and so on, and then, about thirty
levels deep, something goes wrong. Something very bad, like a
network operation failing because the network's just gone down.
What happens? Does your program print a message and exit
altogether? That would be annoying to ordinary users and would be
disastrous if it happened to be an air traffic control system. Does
your function return an error code? In that case, the function
calling it would also probably have to return an error code, and so
on and so on, right back up until some function knows how to deal
with the error - imagine how much checking for error codes you need
to write, and how many mistakes you can make. You need a way of
jumping from the tiny function at the bottom all the way up to the
code that can handle the problem, like a '<tt class=
"literal">goto</tt>', but it needs to be a nice '<tt class=
"literal">goto</tt>' that cleans up the local variables, calls
destructors, and so on. Such a thing is called an exception; the
inner function throws an exception and the outer function catches
it. (Exceptions that are not caught by anything do cause the
program to terminate abnormally)</p>
<p>But how do you pass a message from the inner function to the
outer one, saying what is wrong? Come to that, how do you tell
which outer function should catch the exception? There must be
different types of exception, because there are different types of
things that can go wrong, and you might want different code to be
called depending on what has gone wrong. Indeed, for some things
that go wrong, you might be able to handle them only one or two
functions up the call stack, but for other things you might not be
able to handle them until you get back to the main program. So the
exception throwing mechanism might jump to different places
depending on what has gone wrong.</p>
<p>Obviously, you can't just throw &quot;an exception&quot;. You have to
throw a particular type of exception, and that exception needs to
be able to contain arbitrary information that you have specified.
Sounds familiar? Yes, you can throw an instance of a <tt class=
"literal">struct</tt> or a class. (You can also throw primitive
types and pointers, but classes are most useful when you have a
reasonably complex exception mechanism in your program.) You can
catch exceptions according to what class they belong to, and
inheritance can really come into its own here. For example, some
code can say &quot;I want to handle all network exceptions but I can't
handle anything else&quot;, and some other code might say &quot;I only want
to handle a packet-loss network exception&quot;. The exception will
propagate up the call stack until some code can handle it.</p>
<p>Streams: This is a feature of C++ but not of Java. A stream is
any I/O device that processes characters sequentially - it could be
a keyboard, some text on the screen (or in a window), a file, a
terminal, a printer, a string in memory, or something else (there
is obviously an inheritance mechanism going on here). You can give
any object you want a method to write its data out to a stream, or
read it in from one. Then you can just say (effectively), &quot;write
this object to the printer, get that one from this file&quot; and so on.
Writing formatted output to the screen with streams in C++ is much
safer than doing it in C with functions like <tt class=
"methodname">printf()</tt>, since with <tt class=
"methodname">printf()</tt> in C you have to firstly put in the
format string that you're going to give it an integer (say), and
then actually give it an integer. If for some reason you get it
wrong (or you later change another part of the program to make that
variable no longer an integer), then the compiler does not
(usually) tell you and the program behaves strangely or crashes at
runtime. This is one of the nasties in C, but with C++ streams it's
easier.</p>
<p>Concurrency: Most programs these days have to do several things
at once. For example, a responsive user interface needs to accept
the user's commands whether or not it's also trying to do some big
calculation, or wait for some traffic on the network. An animation
program, or even more so a game, might need to move tens or
hundreds of objects at the same time but at different speeds.
Sometimes it is much easier to write each object as though it were
the only object in the whole program, and write a separate
scheduler later that calls its methods, especially when you come to
write a new type of object and want to quickly integrate it with
the program, or have a variable number of objects that you don't
know in advance (this happens quite a lot in games), or you find a
better scheduling algorithm and want to re-write the scheduler
easily without having to change any of the other classes.</p>
<p>Real threads (as supported by Java but some implementations are
not too good) really do allow more than one part of your program to
be running at the same time on multiple-processor machines, and can
simulate it on single-processor machines. You can create an object
and tell it to go off and do something while you get on with
something else - you don't have to wait for it to finish until you
need its results. Different threads can independently be calling
the various methods of your objects, although you do need to
consider various locking mechanisms to make sure that two or more
of them don't happen to try conflicting operations simultaneously,
and you also need to guard against such things as deadlock, which
is when two or more threads are each waiting for each other. Again,
threads are something that you might want to use sometimes, but
there is little point in using them just to &quot;show off&quot;.</p>
<p>There are many other uses for object orientation, but perhaps
the best ones are those that have not yet been discovered. The
<tt class="literal">struct</tt> (and its sibling, the <tt class=
"literal">class</tt>) is not just a way of grouping variables and
functions together For Some Strange Reason. It is a powerful idea
that led to the development of a whole new programming paradigm and
great leaps forward in design possibilities. But in order to use
object orientation effectively, you need to be familiar with the
design paradigm, not just the syntax. Someone in C Vu once
illustrated this by saying that, if a Victorian carpenter were
given an electric drill, he might use it as a hammer, which would
not be ideal because a power drill makes a lousy hammer . But if
you become familiar with good object-oriented design and it becomes
second nature to you, you will be much more capable and one day you
will wonder how you ever managed without classes and <tt class=
"literal">struct</tt>s.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
