    <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 and Answers</title>
        <link>https://members.accu.org/index.php/articles/1034</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">CVu Journal Vol 12, #4 - Jul 2000</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/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/c125/">124</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 and Answers</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 06 July 2000 13:15:38 +01:00 or Thu, 06 July 2000 13:15:38 +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>I know that most if not all the questions that get posed in this
column have been answered either privately or elsewhere long before
you get your chance. However, contributors gain many benefits from
their efforts. So do readers and those posing questions.</p>
<p>Getting a question into a comprehensible form often results in
the answer being 'obvious'. Formulating a written answer to a
question often leads to unanticipated insights. I know that many
readers have severe demands on their time, but a few minutes (OK,
half an hour) developing a response while waiting for a meeting,
stuck in a traffic jam etc. will be an investment.</p>
<p>On the other side, if you find yourself checking reference books
or asking your local Guru for help, you have a question worth
presenting here. You could even provide both question and your
tentative answer. You might then be surprised by how often your
local expert knows less than you thought.</p>
<p>Please note that questions remain open. In other words, if you
have something to add to an earlier answer, or even realise that
you can answer an earlier unanswered question, please put fingers
to keyboard, or get talking to your voice recognition software.</p>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e28" id="d0e28"></a>Some
Answers</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e31" id="d0e31"></a>Question 3 (C Vu
12.3)</h3>
</div>
<p>Can someone please explain in detail what the -&gt; (arrow
symbol) does in C++.</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e36" id="d0e36"></a>Answer From the
Harpist</h3>
</div>
<p>Let me start by looking back at its origin in C. At that stage
it was completely unnecessary from the language aspect. There was
no need for it. The dot operator could have met all your needs.
Applying the latter to an object would result in the following
member being selected, applying it to a pointer to object would
still result in the following member being selected. The compiler
would know if it were applied to an object or to a pointer.
However, programmers take time to become comfortable with such
built-in operator overloading (even though it is not significantly
different from the way the arithmetic operators vary their
implementation according to context)</p>
<p>Let me give an example:</p>
<pre class="programlisting">
struct X {
  int a, b;
} x;
int main(){
  struct X * x_ptr = &amp;x;
  x.a = 3;    // fine
  (*x_ptr).b = 4;  // fine
  *x_ptr.b = 5;  // error, wrong operator precedence
  x_ptr.a = 6;  // error x_ptr is not an X
  return 0;
}
</pre>
<p>Effectively, the arrow operator was introduced into C as a
shorthand way of writing <tt class="literal">(*x_ptr).b</tt> as
<tt class="literal">x_ptr-&gt;</tt> and eliminate errors such as
the first one above which may not always be diagnosable (think of
having a pointer as a member). However, apart from human
discomfort, there was no reason why the second error should not
have been legitimised by simply defining that applying the dot
operator to a pointer resulted in dereferencing followed by
selection.</p>
<p>The decision made by the designer of C (Dennis Ritchie) proved
to be of tremendous value, not in C but in its offspring, C++.</p>
<p>C does not provide any mechanism for users to define overloads
for operators (perhaps it should be considered, in a limited
fashion, for a future release) but C++ does. Without the existence
of the arrow operator C++ would have been much poorer. Had C taken
the alternative route indicated above, legacy code would have
locked C++ into raw, C-style, pointers. However, given this almost
useless C-operator C++ has been able to build a powerful
superstructure.</p>
<p>When your compiler comes across and arrow in your code it checks
to see if the left-hand operand is a pointer or an object (exactly
the way that C could have checked the dot operator). If it is a
pointer, it first dereferences it (gets hold of the thing pointed
to) and then selects the member of that object designated by the
right-hand operand. In other words, it treats it exactly the way C
would. The fun comes when the left-hand operand is an object. In
this case it has to search the type of that object for a
declaration of <tt class="methodname">operator-&gt;</tt>, and apply
that to the left-hand object. Now it has to check what results, if
it now has a pointer it passes that and the right-hand operand to
the built-in arrow operator (the C-style version).</p>
<p>However, if the return is another object (value or reference) it
repeats the procedure of finding a user-defined <tt class=
"methodname">operator-&gt;</tt>. Fortunately, this kind of
recursive application of user-defined arrow is rare.</p>
<p>The advantage of all this complexity is that programmers can
develop smart pointers. These are operators that have pointer like
behaviour plus something extra. A good (bad from some perspectives)
example of such a type is the C++ Standard Library's <tt class=
"classname">auto_ptr&lt;&gt;</tt>. However, smart pointers do not
have to be templates, though if you are going to go through all the
pain of developing one you might as well maximise the payback.</p>
</div>
</div>
<div class="sect1" lang="en">
<div class="titlepage">
<h2><a name="d0e72" id="d0e72"></a>More
Questions</h2>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e75" id="d0e75"></a>Question 1 from
Robert Lytton <tt class="email">&lt;<a href=
"mailto:robert.lytton@crossprod.co.uk">robert.lytton@crossprod.co.uk</a>&gt;</tt></h3>
</div>
<p>My opinions on using <tt class="literal">const</tt> and when to
use it are still very fluid but here are my thoughts so far.</p>
<p>I have had to use a library in the past that have given function
prototypes in the style <tt class="literal">int AmmendString( char*
, char*, int);</tt> The library was documented but it would have be
nice to have some documentation in the prototype itself, <tt class=
"literal">int AmmendString(char* line, char* word, int count);</tt>
It would also have liked a clue to whether the function alters the
data pointed to? By adding <tt class="literal">const</tt>,
<tt class="literal">int AmmendString(char* line, const char* word,
int count);</tt> it allows me to assume that the data pointed to by
word will not be altered [pointer to const string] while that of
line 'may' be altered!</p>
<p>What if the function was long and involved, are the parameters
valid throughout the function? If I needed to add some
functionality I would have to check carefully that they were not
altered, <tt class="literal">- count;</tt> and if I altered them I
may cause problems latter. By making the parameters <tt class=
"literal">const</tt>, <tt class="literal">int AmmendString( char*
const line, const char* const word, const int count);</tt> we can
rely the parameters not changing [<i class=
"parameter"><tt>line</tt></i> is a <tt class="literal">const</tt>
pointer to a <tt class="type">string</tt>, <i class=
"parameter"><tt>word</tt></i> a <tt class="literal">const</tt>
pointer to a <tt class="literal">const</tt> <tt class=
"type">string</tt> and <i class="parameter"><tt>count</tt></i> is
<tt class="literal">const</tt> too], unless someone has chosen to
cast the <tt class="literal">const</tt> away.</p>
<p>Could the use of <tt class="literal">const</tt> help a compiler
optimise code?</p>
<p>In C++ the return type is made <tt class="literal">const</tt>
when it is an rvalue, <tt class="literal">const Type operator+(
const Type&amp; lhs, const Type&amp; rhs );</tt> for example:</p>
<pre class="programlisting">
Type a, b, c; a+b = c;
</pre>
<p>If <tt class="literal">a+b</tt> returns a <tt class=
"literal">const</tt>, an error will be generated However it seems
that when returning a built in type the <tt class=
"literal">const</tt> is not generally used...</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e167" id="d0e167"></a>Question 2 from
Aaron Ridout <tt class="email">&lt;<a href=
"mailto:aaron.ridout@Signal.co.uk">aaron.ridout@Signal.co.uk</a>&gt;</tt></h3>
</div>
<p>Am I being stupid, or does this:</p>
<pre class="programlisting">
std::cout &lt;&lt; &quot;Some Text &quot; &lt;&lt; std::ios::hex 
           &lt;&lt; x &lt;&lt; std::endl ;
</pre>
<p>take longer to execute than this:</p>
<pre class="programlisting">
printf( buffer, &quot;%s %x\n&quot;, &quot;Some Text &quot;, x ) ;
</pre>
<p class="c2"><span class="remark">I am not going to answer this
now, but there are several issues that readers might like to
consider. The first is the impact that C++'s default support for
mixing C and C++ usage of the standard streams. The second is the
degree to which problems are caused by the strategy of current
implementations of the C++ Standard Library. FG</span></p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e185" id="d0e185"></a>Question 3 from
Aaron Ridout</h3>
</div>
<p>I use the STL containers: <tt class="classname">list</tt>,
<tt class="classname">vector</tt>, <tt class="classname">map</tt>
etc. quite a lot, and often in my designs where there are two or
more threads, one ends up with a container, such as <tt class=
"classname">std::list&lt;&gt;</tt>, to balance out communications
between the two threads... Now what I would like to do is add one
of these wonderfully useful sentry classes to the <tt class=
"classname">std::list</tt> so that it was endowed with thread
safety.</p>
<p>How do I do this?</p>
</div>
<div class="sect2" lang="en">
<div class="titlepage">
<h3><a name="d0e207" id="d0e207"></a>Question 4 from
anon</h3>
</div>
<p>In Java I am used to providing exception specifications and find
them essential. What is the problem with them in C++?</p>
</div>
</div>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
