    <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  :: Questions &amp; Answers</title>
        <link>https://members.accu.org/index.php/articles/894</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">Letters to the Editor + 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/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/articles/c186/">LettersEditor</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/c186-131/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/articles/c186+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;Questions &amp; Answers</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="d0e20" id="d0e20"></a>Question from:
Nick Wedd <tt class="email">&lt;<a href=
"mailto:Nick@maproom.co.uk">Nick@maproom.co.uk</a>&gt;</tt></h2>
</div>
<p>Dear Francis,</p>
<p>I am writing about your article in the April issue of EXE.</p>
<p>Usually, I find your articles interesting, and I am able to
understand them. Rarely, they are of no interest to me and I do not
try to understand them. But while this one is definitely
interesting to me and relevant to my work, I have still failed to
understand it even at a third reading.</p>
<p>You say &quot;the STL is value-based&quot;. What does this mean? What
would the alternative be?</p>
<p>You say &quot;Our card class cannot be used in an STL collection&quot;.
You do not say what would happen if we tried to use it in one.</p>
<p>In my code I define my own classes, and use them in the STL
without problems. If I define a class <tt class=
"classname">wombat</tt>, I find I can have a list or an array of
<tt class="classname">wombat</tt>s, or of <tt class=
"classname">wombat</tt>*s. I suppose I could have a list of
<tt class="classname">wombat</tt>&amp;s, but I still don't feel
comfortable with references.</p>
<p>Of course, whenever I define a class, I write a destructor, a
copy-constructor, and an assignment operator. I feel it would be
asking for trouble to leave them out. I guess that if did leave
them out, all sorts of things would not work: I think I would not
be able to pass them as arguments and use them as return
values.</p>
<p>In the sample code at the bottom right of page 47 (see sidebar),
I cannot understand how the line:</p>
<pre class="programlisting">
answer = Metre(len1-len2);
</pre>
<p>can work. <tt class="methodname">Metre::operator-</tt> has not
been defined. Does the compiler somehow decide that it had better
cast the <tt class="classname">Metre</tt>s to <tt class=
"type">double</tt>s before doing arithmetic on them? If <tt class=
"methodname">Metre::int()</tt> had been defined as well, how would
it know which cast to use?</p>
<p>I would be grateful if you could help me to understand this.</p>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e71" id="d0e71"></a>Reply from Francis
Glassborow</h3>
</div>
<p>This letter raises several interesting points as well as
demonstrating something often missed by technical writers, even
intelligent and diligent readers can face problems if they lack the
assumed foundation knowledge.</p>
<p>I have a rigid definition of a pure value type - it has a public
copy constructor and copy assignment operator (these can be either
implicit or explicit). On the other hand a pure object type for me
is one that has neither of these as public properties. A pure
object has a location (a fixed address) and can only be passed by
reference or accessed through a pointer.</p>
<p>Now all the facilities (algorithms and member functions of the
template containers) provided by the STL function by copying items
in the containers. In other words the items must be copyable - in
my terms they must be values because they must be movable.</p>
<p>We could have had collections that held items via references had
C++ references been like Java ones (reseatable). We can use
collections of pointers to handle objects. But in this case note
that we would have had to have had a very different design
depending on whether we chose to have the collections own the
object pointed at (i.e. responsible for deleting them) or not.</p>
<p>The choice made by the designers of the STL is the most
versatile, but it also requires a greater degree of understanding.
You have to be very careful of collections of pointers. You are
probably better off with some form of smart-pointer with true copy
semantics (<tt class="classname">auto_ptr</tt> will not do because
of the ownership transfer semantics of its copy facilities) if you
want to handle collections of objects.</p>
<p>It is probably hard to imagine how the STL could have been
anything other than value based, but none-the-less it is important
to explicitly understand this property if you are to use the STL
correctly.</p>
<p>Now to that other question.</p>
<p>C++ provides two types of conversion, explicit ones via casts
(either C-style, function style or new-style) or implicitly. For
example:</p>
<pre class="programlisting">
int main (){
  double d = 1.0;
  d = (double) 2;  // C-style
  d = double(2);    // function style
  d = static_cast&lt;double&gt; (3);  // new-style
  d = 4;          // implicit
}
</pre>
<p>Note that the other new-style casts do something rather
different and are not relevant here.</p>
<p>Implicit casts require the existence of either a standard
conversion (such as those for the built-in types, derived to bass
class etc.) or a sequence of such including not more than one user
provided conversion. User provided conversions are of two
kinds:</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>A conversion operator</p>
</li>
<li>
<p>A single argument constructor that has not been declared as
explicit.</p>
</li>
</ul>
</div>
<p>There are other possible conversions available for the three
types of explicit cast but they are not relevant here, all you need
to note is that explicit conversions are a strict superset of
implicit ones. Also note that conversions are applied to values,
not objects. Whenever the compiler is trying to resolve an
expression it is free to try implicit conversions if no
operators/functions have been supplied for the types of the
constituent parts.</p>
<p>Let us now examine the assignment statement in question:</p>
<pre class="programlisting">
answer = Metre(len1-len2);
</pre>
<p>Consider the subexpression <tt class="literal">(len1-len2)</tt>.
The compiler checks to see if <tt class="methodname">operator-</tt>
has been provided for <tt class="classname">Metre</tt> types. It
hasn't. So next it checks to see if there are any implicit
conversions for <tt class="classname">Metre</tt> that convert
values of <tt class="classname">Metre</tt> type to one for which an
<tt class="methodname">operator-</tt> has been defined. If it finds
several possibilities, there are rules to try to resolve the
choice. In this case it finds the conversion operator from
<tt class="classname">Metre</tt> to <tt class="type">double</tt>.
It applies it to both <tt class="varname">len1</tt> and <tt class=
"varname">len2</tt> and completes the subtraction to get a
<tt class="type">double</tt> value.</p>
<p>Now if there were no cast it would look to see if there were an
implicit conversion from <tt class="type">double</tt> back to
<tt class="classname">Metre</tt>. However the constructor that
could do this conversion has been qualified as <tt class=
"literal">explicit</tt>.</p>
<p>The function style cast (constructor call) provides the explicit
conversion from <tt class="type">double</tt> to <tt class=
"classname">Metre</tt> so that the resulting value (temporary
constructed by the explicit call) can be assigned to answer.</p>
<p>It is this liberty to search for conversion sequences (including
at most one user defined conversion) that makes it so dangerous to
provide conversion operators and single argument constructors that
have not been qualified as explicit.</p>
<p>For a moment, consider the consequences of removing the
<tt class="literal">explicit</tt> qualifier from the constructor
for <tt class="classname">Metre</tt>:</p>
<pre class="programlisting">
int main(){
  Metre length =5.0, breadth = 2.0;
  Metre perimeter;
  perimeter = length * breadth;
  cout &lt;&lt; perimeter;
}
</pre>
<p>I know the calculation is wrong but the compiler will not trap
it because we will not have ensured that square metres cannot be
implicitly converted to metres. Of course this is a pretty poor
example but I hope it gives you an idea as to the dangers of
providing implicit conversions for user defined types.</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
