    <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  :: Friends - who needs them?</title>
        <link>https://members.accu.org/index.php/journals/604</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>


        <h2>Journal Articles</h2>


<div class="xar-mod-head"><span class="xar-mod-title">Overload Journal #6 - Mar 1995 + Programming Topics</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/journals/">All</a>

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c78/">Overload</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c177/">06</a>
                    (8)
<br />

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

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

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

                                            <a href="https://members.accu.org/index.php/journals/c177-65/">Any of these categories</a>

                    -                        <a href="https://members.accu.org/index.php/journals/c177+65/">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;Friends - who needs them?</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 27 March 1995 18:22:18 +01:00 or Mon, 27 March 1995 18:22:18 +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="d0e16" id="d0e16"></a>Getting rid of
friends</h2>
</div>
<p>I am writing this article because I find myself out of step with
just about every book and article that I have seen published on the
subject of using the friend access facility in C++. It seems that
there must be something that I have missed or else many of those
masters of C++ that I respect share a blind-spot. Of course the
situation is compounded by all those who dash into print without an
original thought to their name. If you think that last remark is a
bit strong, I think that it is long past time that some of the big
name authors of books on C from the mid-eighties gave their
word-processors a rest and returned to writing some code. When they
have done that they just might be qualified to lecture the rest of
us on writing genuine C++ rather than C for C++ compilers.</p>
<p>Actually, in many cases their C code is pretty tatty and has a
strong odour of the methods of the late seventies when it was
considered quite normal to use implicit conversions between ints
and any and all kinds of pointers.</p>
<p>Self taught programmers often rely heavily on the code they find
in books as being good examples of how a language should be
written. It may be slightly amusing that the classic 'Hello World'
program lacks a return statement but that is not an excuse for
continuing to promulgate such sloppy coding. Properly written C/C++
functions should have explicit return statements marking all exits
from a function. I am not an adherent to the school of programming
that demands a single exit point for each function as I believe
that causes a contorted coding style that is often difficult to
maintain. However I do believe that every terminal point in a
function should be marked with a return statement even if the
programmer believes that no legal path will ever finish there.</p>
<p>Now having alienated any friends that I may have among those
writing about C and C++, I can happily proceed with the topic of
this article in the happy certainty that many will leap to correct
the errors in my thinking without bothering to wrap it up in gentle
phrasing - if you want the truth from someone, make them mad.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e27" id="d0e27"></a>Typical use of
friend</h2>
</div>
<p>For the purposes of this article I am going to use a trivially
simple ADT. I am not going to distract you with all the unnecessary
fripperies but provide just enough flesh to make my point.</p>
<p>Record is a class that encapsulates two pieces of information, a
name and a number (you can assign any significance to these two
items that you like. Here is a pretty minimal
interface/implementation:</p>
<pre class="programlisting">
class Record {
private:
  int number;
  char * name;
protected:
  void put(int i){number=i; return;}
  void put(char const * const cp){
    delete [] name;
    name=new char[strlen(cp)+1];
    strcpy(name,cp);
    return;
  }
  int get() const { return number;}
  void get(char * cp) const {
    // check that cp is a null
    // pointer before changing it
    assert (!cp);
    cp=new char[strlen(name)+1];
    strcpy(cp,name);
    return;
  }
public:
  Record(int i=0; char * cp=&quot; &quot;)
  : number(i), name(0){
    put(cp);
  }
  ~Record(){delete[] name; return;}
}
</pre>
<p>Please do feel free to write and criticise the above code, but
do so constructively by presenting your alternatives with a
rationale. In order to do so you will need to decide why I have
chosen to provide a protected interface for my access functions
while keeping the data strictly private. The implication of the
protected interface is that I intend to permit and support
derivation from Record. Why do you think that I have avoided the
common practice of making my data protected? This is another place
that I disagree with most other writers.</p>
<p>So far my class Record provides access to classes derived from
it and provides a constructor and destructor. I hope you pencilled
in the lack of a copy constructor and overloaded assignment when
doing your code inspection. If you missed that you really need to
be more careful with other people's code. In this case I think that
neither should be implemented because I do not like duplication of
records in databases. So both should be declared private and not
implemented (for the inexperienced this means that attempts to
either copy or assign objects of type Record outside the
implementation of Record will be caught by the compiler, while
attempts in code implementing Record will be detected by the
linker.</p>
<p>I almost wrote in-class and out-of-class in the last sentence
but this would not be true. Not all code implementing a user
defined type will be in the class. There is more to developing a
type than just writing a class interface and implementation.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e42" id="d0e42"></a>Printing
yourself</h2>
</div>
<p>It would seem fairly attractive to provide a mechanism whereby a
Record can send itself to an output stream. At this stage most
writers comment that it would be nice to arrange that our user
defined type should have the same ability that built-in types have
- output by using operator &lt;&lt; and an ostream object. Then
they promptly dig a hole and bury themselves.</p>
<p>They carefully (and accurately) explain why we cannot provide a
new overloaded version of operator &lt;&lt; inside the Record
class. The critical operand (left-hand one) is the wrong type, one
over which we have no rights. The consequence is that the new
overload of operator &lt;&lt; must be provided in global space (I
will avoid the complications of namespaces until they are more
widely implemented). This causes a problem of access to Record
data. What they all finish up with is placing the following line in
class Record:</p>
<pre class="programlisting">
friend
ostream&amp;
operator&lt;&lt;(ostream&amp; out, const Record&amp; r);
</pre>
<p>Why? I just cannot imagine. It is quite unnecessary. Worse
still, as we shall see, it is a poor solution for any class where
the designer intends to allow derivation and possible late
binding.</p>
<p>What do I do? Look at the following prototype for a public
member function.</p>
<pre class="programlisting">
void print(ostream&amp; out = cout) const;
</pre>
<p>Implement it any way you want. As it is an output function you
probably do not want to fatten your code by making it inline, but
do so if you must. Having done this, you can either implement an
overloaded version of operator &lt;&lt; yourself or leave it up to
users of your code to do so. The implementation is about as simple
as you can get:</p>
<pre class="programlisting">
ostream&amp;
operator&lt;&lt;(ostream&amp; out, const Record&amp; r) {
  r.print(out);
  return out;
}
</pre>
<p>All the functionality you had before but no abuse of friendship.
If this is as far as it went I would not be quite so disturbed by
what I read but ...</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e63" id="d0e63"></a>friend and
polymorphism do not mix</h2>
</div>
<p>Of course my class Record is pretty skimpy both in data and
functionality. I can be certain that I am going to want to use it
as a base class. In real life, I would have written an abstract
base class (ABC) with no data and only pure virtual member
functions to provide an interface. But getting completely abstract
inhibits some programmers ability to understand.</p>
<p>Go back to my class Record and make the destructor virtual, at
the same time add virtual to the prototype for print(). Do you see
the result? Exactly, I now have a polymorphic (late binding)
implementation of operator &lt;&lt; for my hierarchy of classes
derived from Record.</p>
<p>Surely this must be the right way to go? Using 'friend' to fix
up the access problem is not only unnecessary but positively gets
in the way of finding an effective object-oriented solution.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e72" id="d0e72"></a>Mixins and
printable</h2>
</div>
<p>There is one further step to take down the path to full OO
programming. You need to grasp the concept using multiple
inheritance with ABC's to implement a style that is called mixin
programming (in memory of American Ice Cream vendors - British
programmers might use the term &quot;pick 'n mix&quot;). Consider the
following minimalist class:</p>
<pre class="programlisting">
class Printable {
public:
  virtual void print(
  ostream&amp; out = cout) const = 0;
  virtual ~Printable(){};
}
</pre>
<p>Printable is an ABC that does nothing other than encapsulate the
concept of being printable. The reason that I have included a
virtual destructor is because sometime someone is going to want to
create a collection of printable objects.</p>
<p>Now consider the effect of:</p>
<pre class="programlisting">
ostream&amp;
operator&lt;&lt;(ostream&amp; out, const Printable&amp; p)
{
  p.print(out);
  return out;
}
</pre>
<p>If we want to provide any class with the facility to use an
overloaded operator &lt;&lt; we have to ensure that it has
Printable as a base class and a member function void
print(ostream&amp;).</p>
<p>Once you grasp the value of this type of ABC, one that provides
the interface for a specific capacity that a user defined type may
have you are ready to use a mixin style.</p>
<p>Multiple inheritance is an essential ingredient for such a
style. For example another essential property for class Record
would be persistence (the ability to write and read itself to/from
backing storage). In this case you might decide that the ABC had
some data. For example you might decide that it is a property of
persistent objects that they know if they have been archived.</p>
<p>Now our class Record needs two independent base classes, and we
can see how we need to have multiple inheritance available. For
example:</p>
<pre class="programlisting">
class Record: public Printable, public Storable
{
// class interfaces
}
</pre>
<p>Inexperienced programmers often argue that they can provide the
functionality of base classes by substituting aggregation or
layering for inheritance. That is by providing a Printable and a
Storable member of class Record. It should be clear that this will
not work for mixin programming. The mixin elements are ABCs and so
objects of exactly that type can not exist (though, of course,
objects derived from them can). Much more important than this
restriction (which could be fixed by removing the pure virtual
functions) is the fundamental need for late binding which cannot be
provided other than by inheritance.</p>
<p>If you are simply intending to use multiple inheritance to build
the interface of the proper base for your hierarchy then the above
methodology will meet all your needs. In reality, once multiple
inheritance is available it will be used for other purposes -
because it is actually useful and not just a fad. This leads to the
problem of cases such as:</p>
<pre class="programlisting">
class X1
: public isPrintable1, public anotherPrintable
{
// class interfaces
}
</pre>
<p>Now X1 has two base classes Printable. The introduction of
virtual base classes exactly fixes this problem with very little
overhead if the virtual bases are ABCs with minimalist
interfaces.</p>
<p>Almost always when you are using mixins, they should be virtual
public base classes. You should not feel reluctant to use this
style. Multiple inheritance, virtual bases and ABCs are a powerful
mix that all programmers should have in their toolkit of
programming methods.</p>
<p>Like friend access, multiple inheritance, virtual bases and ABCs
are useful but should not be hijacked to do things that do not need
them. Such hijacking produces code that is difficult to maintain
and probably does not do what you want.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e107" id="d0e107"></a>Work with
your language</h2>
</div>
<p>One of the things we teach dinghy sailors is to learn to work
with the elements and not try to fight them. If your boat
constantly resists you then you are probably trying to achieve your
aim by inappropriate methods. The eventual result is a 'swimming
lesson'.</p>
<p>If you constantly seem to be struggling with your chosen
programming language it is either the wrong language for the
problem domain or you do not understand how to use it. The result
of such a struggle is poor code, riddled with defects which is hard
to maintain.</p>
<p>Learn to work with the tools of your language to express
coherent solutions to your problems.</p>
<p>Now perhaps the 'friend' aficionados will stand up and explain
why I am wrong. They must have an explanation because many of them
take considerable sums of money from the ordinary punters for books
that use it to supply global operators.</p>
<p>Even worse are those that charge large 3 figure (or even four
figure) fees to teach you C++ and still teach 'friend' as the way
to go.</p>
<p>There are important uses for friend, without which much code
could not be written in a clear and effective fashion but that is
another article or two.</p>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
