    <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  :: Micro-Derivation &amp; Related Ideas</title>
        <link>https://members.accu.org/index.php/articles/536</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 + Overload Journal #32 - 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/c78/">Overload</a>

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

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

                    -                        <a href="https://members.accu.org/index.php/articles/c65+172/">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;Micro-Derivation &amp; Related Ideas</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 June 1999 17:50:53 +01:00 or Sat, 26 June 1999 17:50:53 +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="d0e14" id="d0e14"></a></h2>
</div>
<p>I have been very busy these last few weeks and have not had time
to think about what I might write for Overload. I know what
pressure there is on editors and how much they rely on the regular
contributors coming up with something for each issue. Of course, as
Francis frequently comments, there would not be so much pressure if
there was a steady flow of material from elsewhere. Fiction
magazines normally have large slush piles on which they can draw
for the fillers they need to complete an issue. That gives new
writers a chance as well as guaranteeing the readers a regular mix
of old and new talent.</p>
<p>I had just about decided, reluctantly, to give this issue a miss
when Francis asked me to look at some ideas he was playing with.
What follows leans heavily on his ideas and is entirely
speculative. In other words I have not had time to test even a line
of code let alone explore how the ideas might work in detail. I
hope, that you, the reader will explore some of these ideas and
feed back your experiences, both good and bad.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e20" id=
"d0e20"></a>Micro-Derivation</h2>
</div>
<p>This is a term that I have coined to refer to using derivation
to add some small extra to a class while leaving the class
functionally as it was before. I think this is best illustrated by
a simple example.</p>
<p>Have you ever wanted to have a simple array of objects only to
discover the class owner has decided not to support a default
constructor? Well if you never have, you have been lucky. Look at
the following:</p>
<pre class="programlisting">
class HisObject {
  &lt; no default constructor available&gt;
};

int main(){
  HisObject objects[10];
  // other code
}
</pre>
<p>Of course your compiler refuses to compile this. In this
circumstance you can just about manage by providing explicit
initialisation such as:</p>
<pre class="programlisting">
HisObject objects[10] = {
  HisObject(&lt;suitable data&gt;),
  HisObject(&lt;suitable data&gt;),
  HisObject(&lt;suitable data&gt;), 
  // etc. for 10 instances
};
</pre>
<p>However that would be intolerable if you had a large array and
in some circumstances (array member of a class) it is
impossible.</p>
<p>Maybe you are wondering why I do not use an STL type container
instead. There are several reasons why I might choose an old style
array. For example I might need the efficiency that these
guarantee. I might also have difficulty if the <tt class=
"classname">HisObject</tt> class lacks strict copy semantics. In
this case it might have had copying inhibited (I will visit that
shortly) or it might actually have had some semantic aspect of
copying perverted (see <tt class="classname">auto_ptr</tt>). Either
of these two cases means that STL containers are dangerous.</p>
<p>So instead I use micro-derivation:</p>
<pre class="programlisting">
class MyObject : public HisObject {
public:
  MyObject();
};
</pre>
<p>The first reaction from experienced programmers is that the
original class designer probably had a good reason for not
providing a default constructor. I entirely agree. However a good
reason in general might not be relevant in the context of my
program. The only feature I am adding is the ability to create
default instances. Naturally I must ensure that such can be used
effectively.</p>
<p>Some questions that will spring to mind is to ask 'Why public
derivation? Why not either private derivation or layering?' My
answer is that I want <tt class="classname">MyObject</tt> instances
to be exactly what <tt class="classname">HisObject</tt> instances
are with the solitary exception that they can be created by
default. I am completely happy for <tt class=
"classname">MyObject</tt> instances to substitute for <tt class=
"classname">HisObject</tt> ones.</p>
<p>There is one tiny irritant, everything will work as I expect
once the object has been created, but <tt class=
"classname">HisObject</tt> must have some constructors and these
are not inherited. That means that I must add 'forwarding'
constructors in addition to my default constructor. For example,
suppose that <tt class="classname">HisObject</tt> has a constructor
<tt class="methodname">HisObject (int)</tt>, now I must add:</p>
<pre class="programlisting">
MyObject(int i):HisObject(i){};
</pre>
<p>to my class interface. It would have been nice if I could have
written:</p>
<pre class="programlisting">
using HisObject;
</pre>
<p>to mean that I wanted <tt class="classname">MyObject</tt> to
have constructors that did no more than call equivalent HisObject
constructors. As it is I have to implement this by hand. Perhaps a
good idea as it should make me think about what I am doing.</p>
<p>The idea of deriving to add just one feature is quite powerful.
We can take a pure object class (no public copy semantics) and
convert it into a value class as long as we can code a copy
process. [<i><span class="remark">Note from Francis: indeed and
this is the subject of a forthcoming column of mine in EXE
Magazine</span></i>].</p>
<p>I am going to leave this at this stage and invite readers to
come up with other instances where micro-derivation is useful. Note
that I apply the term precisely to the case where you wish to add
one (or possible more) features to a class which otherwise must
behave exactly as it did before. Adding some form of constructor is
the most likely extra.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e92" id="d0e92"></a>Related
Ideas</h2>
</div>
<p>I suppose that you might call the next idea micro-layering. I
first came across the idea when Francis was discussing ways of
providing user defined types that behaved like built-in ones. In a
sense <tt class="literal">enum</tt>s do this for integer types. For
example:</p>
<pre class="programlisting">
enum Int {
  min_value=INT_MIN,
  max_value=INT_MAX
};
</pre>
<p>creates a type that has almost all the functionality of an int.
Unfortunately it lacks any implicit inward conversions. <tt class=
"classname">Int</tt> values will convert to int ones implicitly but
to go the other way you must make the conversion explicit with a
cast. So try this instead:</p>
<pre class="programlisting">
class Int {
  int value_m;
public:
  Int(int v=0): value_m(v) {}
  operator int () {return value_m;}
};
</pre>
<p>I have deliberately not qualified the constructor as explicit
because I want me Int type to behave as like the built-in type as
possible. Once I have a UDT with <tt class="type">int</tt>
functionality I can derive from it, add constraints (for example
declare a private <tt class="methodname">operator*()</tt> and
<tt class="methodname">operator/()</tt> etc.</p>
<p>I wonder what weaknesses there maybe in this idea. Certainly I
can think of several advantages. For example define:</p>
<pre class="programlisting">
class Double {
  double value_m;
public:
  Double(int v=0): value_m(v) {}
  operator double () {return value_m;}
};
</pre>
<p>There is no implicit conversion between <tt class=
"classname">Double</tt> and <tt class="classname">Int</tt> because
that would require two user-defined conversions (one conversion
operator and one constructor).</p>
<p>Our UDTs can work where-ever built-in values are required but
will not satisfy a pointer to built-in type nor a non-const
reference to a built-in type.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e134" id="d0e134"></a>An
Alternative to Micro-Derivation</h2>
</div>
<p>Let me finish be revisiting my problem with <tt class=
"classname">HisObject</tt>. If you are unhappy with using
derivation you could try this instead:</p>
<pre class="programlisting">
class MyObject {
  HisObject obj_m;
  static const HisObject def_s;
public:
  MyObject(HisObject data = def)
    : obj_m(data) {}
  operator HisObject () { return obj_m;}
};
HisObject MyObject::def_s(
  &lt;data for construction&gt; );
</pre>
<p>This assumes that a copy constructor is available for <tt class=
"classname">HisObject</tt>.</p>
<p>I leave it to readers to provide a comparison between
micro-derivation and micro-layering as a mechanism for adding
functionality. One difference is that micro-layering consumes the
permitted explicit user-defined conversion whereas micro-derivation
doesn't.</p>
<p>One of the things that I have become increasingly aware of is
the potential that very small classes have for fixing coding
problems. I wonder what other ideas readers have for useful class
techniques that require less than a dozen lines of class
interface?</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
