    <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  :: When's an object not an object?</title>
        <link>https://members.accu.org/index.php/articles/519</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">Design of applications and programs + Overload Journal #34 - Oct 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/c67/">Design</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/c170/">34</a>
<br />

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

                    -                        <a href="https://members.accu.org/index.php/articles/c67+170/">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;When's an object not an object?</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 26 October 1999 17:50:55 +01:00 or Tue, 26 October 1999 17:50:55 +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>
<p>When is an object not an object? When it's a value! What's the
difference in the way that a std::string and std::cout are treated?
A string is treated as a value, and cout is treated as an object.
What's the difference?</p>
<p>Consider how ints are typically used in C++. You declare them,
you assign them, combine them with operations and convert them to
and from other types of value (string, double, etc). You don't
think of int i = 10 and int j = 10 as being the same thing; they
merely have the same value or contents. You don't worry about
temporaries being created and destroyed when you write 2 + 3 and
neither do you tend to pass ints by const reference. You may write
conversion operators to and from other classes using non-explicit
single argument constructors and operator int(). For your own value
based classes you will implement the copy constructor and the
assignment operator so that you can use your class with the STL.
You will also tend to implement equality testing and the comparison
operators. You will be unlikely to inherit from the class so none
of the methods would be virtual. Also you don't worry about memory
leaks when using such a class (assuming it's been written cleanly)
as you normally declare variables on the stack or use them in
containers. Neither do you allocate them dynamically or use smart
pointers to manage their lifetimes.</p>
<p>In comparison, consider how a window class is typically used.
You don't go around copying windows or comparing their contents, so
you'll declare the copy constructor, the assignment operator and
the equality operator private to prevent their use. You use
operations to change a window but you still consider it to be the
same window. You don't pass them by value, you pass them by
non-const reference instead. You expect to derive other classes
from the window so you make all of the public functions virtual
including the destructor so that you can use polymorphism. Often
you will allocate a window dynamically using new and possibly use a
smart pointer to prevent memory leaks, or rely upon the good
services of a garbage collector to tidy up for you afterwards. You
don't create expressions using windows or write operators that
return temporary windows. Any operators you define will be of the
compound type that modify the left-hand operand rather than
returning a temporary (operator+= instead of operator+). You won't
define any conversion operators and single argument constructors
will be labelled as explicit.</p>
<p>What is the fundamental difference between these two styles of
usage? The first (string, int) has value semantics and the second
(cout, window) have reference semantics. Let's take a look at how a
number of languages deal with this issue. C++ is value based by
default (because of the presence of a default copy constructor and
assignment operator) but supports reference semantics easily. Java
is purely reference based (all variables are references and the
only way of creating an object is new) and has to work hard to
handle values. Eiffel is reference based by default but has
expanded types for values. Smalltalk is purely reference based and
makes no attempt at values, and functional languages such as ML are
purely value based as they have no assignment.</p>
<p>How does this affect the programming environment? Reference
based languages need garbage collectors. Most garbage in reference
based languages is generated by trying to simulate values. This is
why generational garbage collectors are so effective. Values tend
to be short lived and so are cleared up soon, whereas reference
based objects are long lived and so survive many generations. In
value based languages the values are usually stack based or
compiler generated temporaries and so no garbage collector is
necessary. Look at the effort required in reference based languages
to support value based programming: clone and deep_clone operations
and equals and deep_equals methods are often provided by a single
rooted object hierarchy, garbage collection is needed, and certain
arithmetic value types are built-in for efficiency. In contrast
there is very little machinery needed to support reference based
programming in value based languages: all that is needed is
pointers and the ability to redefine copy construction and
assignment.</p>
<p>How does this affect our perception of programming? Values are
simple things and tend to be small (ints, strings, points).
Reference objects are often large (cf cout, windows, databases) and
contain state. When you change the state of a window you tend to do
it incrementally and you don't think of it being a different
window, whereas when you change a string it is completely changed
with no memory of its former contents.</p>
<p>The clean separation of value types and reference types can be
blurred by having different semantics for different interfaces
implemented by the same object. For instance, consider a persistent
string implemented as a subclass of string (containing no virtual
methods) and a subclass of an abstract base class called
PersistentObject (containing only virtual methods).</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e34" id="d0e34"></a>Implementation
consequences and guidelines</h2>
</div>
<p>How does this distinction help us when programming? Deciding
whether we are producing a value type or a reference type tells us
what we should and should not include in our class. Here are some
heuristics. They are not hard and fast rules. They are however
designed to stimulate discussion.</p>
<div class="itemizedlist">
<ul type="disc">
<li>
<p>Don't mix reference semantics and value semantics in the same
interface.</p>
</li>
<li>
<p>Objects with multiple interfaces may use different semantics for
each one.</p>
</li>
<li>
<p>If object copying is disallowed then declare the copy
constructor and assignment operators as private, and make all
methods virtual.</p>
</li>
<li>
<p>If object copying is allowed then define the copy constructor
and assignment operators, and make no methods virtual.</p>
</li>
<li>
<p>Don't create new value types using inheritance, unless adding a
new interface to an existing type.</p>
</li>
<li>
<p>Use value types by naming their class directly.</p>
</li>
<li>
<p>Use reference types only via an abstract interface (use by
type).</p>
</li>
<li>
<p>Operators on reference types should be compound operators,
whereas operators on values can be either compound or simple value
returning ones.</p>
</li>
<li>
<p>Don't allocate values dynamically; create anonymous temporaries
instead.</p>
</li>
<li>
<p>Use dynamically allocated objects only in conjunction with smart
pointers.</p>
</li>
<li>
<p>Use values with smart pointers only if the pointers implement
copy on write to preserve value semantics.</p>
</li>
<li>
<p>Reduce the number of outgoing dependencies of value types,
preferably to zero, to aid in reuse.</p>
</li>
<li>
<p>Don't write equality or comparison operators for reference
types.</p>
</li>
<li>
<p>Don't take the address of a value.</p>
</li>
<li>
<p>Don't write conversion operators for reference types.</p>
</li>
<li>
<p>Pass reference types by non-const reference or pointer and value
types by value or const reference.</p>
</li>
<li>
<p>Return reference types by reference and values by value from
functions.</p>
</li>
<li>
<p>Don't share values; copy them instead to preserve semantics.</p>
</li>
<li>
<p>Make single argument constructors explicit only for reference
types.</p>
</li>
<li>
<p>Conversion operators for value types should return by value or
const reference.</p>
</li>
<li>
<p>On UML diagrams use aggregation (open diamond) for reference
types and composition (solid diamond) for value types.</p>
</li>
</ul>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
