    <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  :: Readers Letters</title>
        <link>https://members.accu.org/index.php/journals/1446</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 #4 - Feb 1994 + Letters to the Editor</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/c233/">04</a>
                    (11)
<br />

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

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c184/">Journal Columns</a>

                     &gt;                         <a href="https://members.accu.org/index.php/journals/c186/">LettersEditor</a>
                    (132)
<br />

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

                    -                        <a href="https://members.accu.org/index.php/journals/c233+186/">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;Readers Letters</h1>
<p><strong>Author:</strong>&nbsp;</p>
<p>
<strong>Date:</strong> 01 February 1994 08:51:00 +00:00 or Tue, 01 February 1994 08:51:00 +00:00</p>
<p><strong>Summary:</strong>&nbsp;</p>
<p><strong>Body:</strong>&nbsp;<p>Hi Mike,</p>
<p>I have a number of questions about the ARM.</p>
<ol>
  <li>How is a data type (as oppose to an object of such a type)&nbsp;
be passed&nbsp; as&nbsp; a&nbsp; function&nbsp; parameter?&nbsp; (e.g.
to implement functions like va_start, va_arg, va_end for variable
number of parameters)<br>
    <br>
    <span style="font-style: italic;">C+ + allows variable numbers of
parameters even though there is no guaranteed way of accessing the
parameters (like passing C structs or pointers to structs). Bjarnes
advice (on pl47 of the ARM) is DON'T USE IT!!. If you are trying to
implement them yourself they are so machine and compiler specific and
involves compiler magic that it virtually impossible.<br>
    <br>
    </span></li>
  <li>If F () is an inline function, do F or &amp;F mean the address of
the function? If so, what is the address of an <span
 style="font-family: monospace;">inline</span> function mean?<br>
    <br>
    <span style="font-style: italic;">Inline is only a hint to the
compiler for optimisation purposes that an inline substitution is
preferred. There is no guarentee that it will actually be placed
inline. Any number of factors could cause the function to be 'not
optimised', the size of the function, whether it calls other functions
(maybe inline themselves). Taking the address of the function would
probably be enough for the optimiser not to inline the function. If you
use inline functions taking the address can give you surprises.<br>
    <br>
    </span></li>
  <li>Is a virtual inline function really inline?<br>
    <br>
    <span style="font-style: italic;">The virtual mechanism is usually
implemented as indirect function calls - I would think that a virtual
inline function is not inlined by the optimiser.</span><span
 style="font-style: italic;"></span></li>
  <li>
    <pre>int x = 14; <br>void main(void) <br>{<br>  int x = ::x + 1;&nbsp; // use :: to access the global x<br>  { <br>    int x=&nbsp;&nbsp;&nbsp; ;//?????<br>  } <br>}</pre>
The initialisation for first local variable x to the value of the
global x is done by using the : : operator. Is there is a similar way
to initialise the second local variable x to the value of the first
local x?<br>
    <br>
    <span style="font-style: italic;">Not that I know. Try using a
different name or adding a reference to the outer x. i.e. int&amp; rx =
x;<br>
    <br>
    </span></li>
  <li>
    <pre>class A { ..... };<br>class B : public A { ..... };<br>B b[10];<br>A * pA;<br>void func(void)<br>{<br>  pA = b;<br>  pA++;&nbsp;&nbsp; &nbsp;// ???<br>  pA =&nbsp; pA + 3; // ??? <br>}</pre>
Are the last two statement defined or valid? What is <span
 style="font-family: monospace;">sizeof(*pA)</span>?<br>
    <br>
    <span style="font-style: italic;">The first (pA++) adds sizeof (A)
bytes to the address contained in pA, the second (pA + 3) adds
3*sizeof(A) bytes to the address in pA. If either of these were
fortunate enough to be correct addresses of the corresponding B items
you would be lucky (and B would contain no data items of its own). Try
the following example</span><br>
    <pre>#include &lt;iostream.h&gt;<br><br>class A<br>{<br>protected:<br>  int val; <br>public:<br>  virtual void display()<br>  {<br>    cout &lt;&lt; &quot;Inside A&quot; &lt;&lt; endl;<br>  }<br>};<br>class B : public A<br>{<br>protected: <br>public:<br>  void set(int a) { val = a; }<br>  virtual void display()<br>  {<br>    cout &lt;&lt; &quot;Inside B[&quot;<br>         &lt;&lt; val<br>         &lt;&lt; &quot;]&quot;<br>         &lt;&lt; endl; <br>  }<br>};<br><br>void main (void)<br>{<br>  B b[10];<br>  {<br>    int i ;<br>    for (i = 0; i &lt; 10; i++)<br>      b[i].set(i); <br>  } <br>  A* pA;<br><br>  pA = b; // points to b[0] <br>  pA-&gt;display(); // prove it <br>  pA++;<br>  pA-&gt;display () ; // prove it <br>  pA = pA + 3; <br>  pA-&gt;display(); // prove it<br>}<br></pre>
    <p><span style="font-style: italic;">As B contains no data of its
own, the output will be:</span><br>
    </p>
    <pre>Inside&nbsp; B[0]<br>Inside&nbsp; B[l] <br>Inside&nbsp; B[4]<br></pre>
    <p><span style="font-style: italic;">If a single data item (int x;)
is added to B's protected section, the output changes. On the compilers
I tested it with, both printed the first line OK, and then hung the PC.</span></p>
  </li>
  <li>
    <p>In order to manipulate bit maps (images) or for other reasons,
it is desirable to enter some constant in binary format. Apparently C
and C++ do not seem to support binary constants directly. I think by
using bit-fields constants can be converted to avoid runtime overhead
and / or manually converting to Hex format.<br>
    </p>
    <pre>struct binary{<br>  int bit0:l,&nbsp; bitl:l,&nbsp; bit2:l,&nbsp; bit3:l, <br>      bit4:l,&nbsp; bit5:l,&nbsp; bit6:l,&nbsp; bit7:l,<br>      bit8:l,&nbsp; bit9:l,&nbsp; bitA:l,&nbsp; bitB:l, <br>      bitC:l,&nbsp; bitD:l,&nbsp; bitE:l,&nbsp; bitF:1; <br>}; <br>union BinToHex{<br>  binary B;<br>  int&nbsp;&nbsp;&nbsp; I; <br>}; <br>BinToHex B2H = {{0, 1, 0, 1,<br>                 0, 1, 0, 1,<br>                 1, 0, 1, 0,<br>                 1, 0, 1, 0}};</pre>
    <p>The questions is that it looks like this method is compiler
implementation dependent and hence not portable, because of the size of
int can vary. Is there a better way to do it:<br>
    <br>
    <span style="font-style: italic;">No! Bitstrings and bitset are
already in the draft standard and a proposal on the table for binary
literals.</span></p>
  </li>
  <li>
    <p>I came across some data types like <span
 style="font-family: monospace;">size_t</span>, <span
 style="font-family: monospace;">time_t</span>, etc., in some header
files. Is it true to say that they are for portability? Should we use
them instead of <span style="font-family: monospace;">int</span>, <span
 style="font-family: monospace;">char</span>, etc. all the time?<br>
    <br>
    <span style="font-style: italic;">Yes. Also ptrdiff_t and clock_t
etc.</span></p>
  </li>
  <li>
    <p>Can constructors and destructors be recursive functions? If they
can, would you give me an example?<br>
    <br>
Yes, but they are not very useful.<br>
    </p>
    <pre>class X <br>{ <br>public:<br>  X(int n)<br>  {<br>    if (n&gt;0) {<br>      *this =&nbsp; X(n-l); <br>    } <br>    else <br>    {<br>      i = 0;<br>    }<br>  } <br>private:<br>  int i; <br>};</pre>
  </li>
  <li>
    <p>On page 41 in CVu July 1993, near the middle of the right hand
column,<br>
    </p>
    <pre>CVuString AString() ;</pre>
    <p>Can this line also mean a function prototype? I suppose the
compiler has to look at the context to decide.<br>
    <br>
    <span style="font-style: italic;">I don't have a copy handy - it
looks like a function prototype to me!</span></p>
  </li>
  <li>
    <p>Can a constructor call other member function that access the
object's data members? I think before the constructor terminates, the
object is considered not fully constructed (initialised). So even if
this is possible to call a member function, there is a risk.<br>
    <br>
    <span style="font-style: italic;">Its perfectly legal and there are
risks. These risks are the same for any uninitialised data items.
Another danger is that if polymorphic member function is called, only
the base member function will be called.</span></p>
  </li>
  <li>
    <p>If a class x has members <span style="font-family: monospace;">new()</span>
and <span style="font-family: monospace;">delete()</span> (<span
 style="font-family: monospace;">new(size_t)</span> and <span
 style="font-family: monospace;">delete (void&nbsp; *, size_t)</span>&nbsp;
?) to maintain free store ( e.g. in order to improve efficiency), <span
 style="font-family: monospace;">delete()</span> collects the freed
memory in a pool instead of return to the system, how does the memory
release back to the system before the termination of the program, or on
request?<br>
    <br>
    <span style="font-style: italic;">Delete does not have size_t.</span><br>
    <br>
    <span style="font-style: italic;">Unless you free (or delete) the
memory it does not get returned to the OS until the program terminates.</span></p>
  </li>
  <li>
    <p>How is the address of a label (for goto) be obtain in C++? Hence
an array of code addresses can be set up for computed jumps. Although I
understand the resulting code may be difficult to read and / or
maintain (just like using goto), under some situations (e.g. to
optimised code), it is desirable to have this ability.<br>
    <br>
    <span style="font-style: italic;">You cannot take the address of a
label. If I find you using goto, you are fired!!!!</span></p>
  </li>
  <li>
    <p>On page 248 of &quot;The C++ programming language&quot; 2nd edition by
Bjarne Stroustrup,<br>
    </p>
    <pre>class string {<br>    :<br>    :<br>  char &amp; operator[](int i);<br>  const char &amp; operator[](int i) const;<br>    :<br>    :<br>};<br><br></pre>
    <p>these two member functions seem to the same signature. Why are
they allowed?<br>
    <br>
    <span style="font-style: italic;">If you have a const string, the
second one will be used; otherwise the first one will be used.</span></p>
  </li>
  <li>
    <p>What is the reason for C compilers to put underscore prefixes to
every identifier? And is this an ANSI standard?<br>
    <br>
    <span style="font-style: italic;">No it isn't a standard. I dont
know why - anybody?</span></p>
  </li>
  <li>
    <p>On page 175 in section 5.5.5 Array of Class objects in the book
&quot;The C++ programming language&quot; 2nd edition by Bjarne Stroustrup, there
says &quot;to declare an array of objects of a class with constructor, that
class must have a default constructor ...&quot;<br>
    <br>
I tried the following on Borland C++ 3.1 and it worked. Is this a
language extension in BC++ 3.1?<br>
    </p>
    <pre>#include&nbsp; &lt;iostream.h&gt; <br>class E{<br>  int x;<br>public:<br>  E(int z) : x(z) {}<br>  int getx(void) {return x;} <br>};<br>E e[5] = {0, 1, 2, 3, 4}; <br>void main(void) <br>{<br>  for (int loop = sizeof(e)/sizeof(E); loop &gt; 0; --loop)<br>  {<br>    cout &lt;&lt; e[loop-1].getx() &lt;&lt; '\n';<br>  } <br>}</pre>
    <p><span style="font-style: italic;">Not sure. But it seems to be a
common extension.</span></p>
  </li>
  <li>
    <p>a) On page 277 of&nbsp;&nbsp;&nbsp; &quot;The C++ programming
language&quot;&nbsp; 2nd edition by Bjarne Stroustrup, half way down, What
is the use of the <span style="font-family: monospace;">typedef </span>statement
in <span style="font-family: monospace;">class Comparator</span>?<br>
    </p>
    <pre>template&nbsp; &lt;class&nbsp; T&gt;&nbsp; class&nbsp; Comparator&nbsp;&nbsp; { <br>public:<br>  typedef T T;<br>  static int lessthan(T &amp; a, T &amp; b){ <br>    return a &lt; b;<br>  } <br>};</pre>
    <p>It looks like the line is trying to (re)define <span
 style="font-family: monospace;">T</span> as <span
 style="font-family: monospace;">T</span>, is this redundant?<br>
    <br>
    <span style="font-style: italic;">No! It ensures that the
comparator class has a member called T which is important if you are
using the comparator class as a template argument.</span><br>
    <br>
b) At the beginning of the same page,<br>
    </p>
    <pre>template &lt;class Comp&gt; class Sort{ <br>public:<br>  class Comp::T;<br>  static void sort(Vector &lt;Comp::T&gt; &amp;); <br>};</pre>
    <p>What is the reason for declaring class <span
 style="font-family: monospace;">Comp: :T;</span>? If the purpose is to
make <span style="font-family: monospace;">static void sort()</span>
possible to declare with parameter type involving <span
 style="font-family: monospace;">Comp::T</span>, sure, <span
 style="font-family: monospace;">Comp</span> and <span
 style="font-family: monospace;">Comp::T</span> must have defined
(declared) before the definition of <span
 style="font-family: monospace;">Sort&lt;&gt;</span>.<br>
    <br>
    <span style="font-style: italic;">Comp is a type which has a member
T which is also a type. This is to be replaced with &quot;typedef Comp::T&quot;
when compilers become ISO compliant.</span></p>
  </li>
  <li>
    <p>Why can't we use <span style="font-family: monospace;">virtual</span>
and <span style="font-family: monospace;">_fastcall</span> function
modifiers together in Borland C++ 3.1?<br>
    <br>
    <span style="font-style: italic;">'_fastcall' modifies the method
of calling a function (or member function) in a manner similar to the
'_pascal specifier. With '_fastcall' functions the compiler attempts to
put all the parameters in registers (a maximum of three parameters may
be passed). Reasons for incompatibility may be that the virtual member
function call probably requires the use of more registers than a normal
call thus making '_fastcall' less useful or that the mechanism for
passing the parameters to all members of the polymorphic hierarchy must
be the same, this might be difficult to ensure (i.e. all polymorphic
member functions would have to be '_fastcall' types. By the way, its
non-portable and changes to '__fastcall' (2 underscores) in BC 4.0!</span></p>
  </li>
  <li>
    <pre>class A { <br>public:<br>  virtual void f(); <br>};<br>class B: public A { <br>public:<br>  virtual void f(int); <br>};</pre>
    <p>Why does Borland C++ 3.1 warns <span
 style="font-family: monospace;">A::f()</span> is being hidden by <span
 style="font-family: monospace;">B::f(int)</span>?<br>
    <br>
    <span style="font-style: italic;">Because it is being hidden. You
cannot call A::f() for a B object as the overload set of B is not a
superset of A for the function f</span></p>
  </li>
  <li>
    <p>When a function with default is called indirectly through
function pointer, e.g.<br>
    </p>
    <pre>void f(int a = 3);<br>void (*pf)() = f;&nbsp;  // is this allowed?<br>(*pf)();&nbsp; (*pf)(l); // how about these 2 calls?</pre>
    <p><span style="font-style: italic;">The type of f is a function
taking int and returning void. The type of pf is a pointer to a
function taking no arguments and returning void. The initialisation of
pf is not allowed because they are different types. A default argument
affects how a function is called and not its type.</span><br
 style="font-style: italic;">
    <br style="font-style: italic;">
    <span style="font-style: italic;">Of the last 2, the first call is
undefined, the second is ill-formed.</span></p>
  </li>
  <li>
    <p>In class&nbsp; D below, are both the constructors for class A or
class&nbsp; B called?<br>
    <br>
Is the object d not stable?<br>
    </p>
    <pre>class A{<br>  :<br>  :<br>  A();<br>};<br>class B{<br>  :<br>  :<br>  B(); <br>} <br>union C {<br>  A a;<br>  b b; <br>}; <br>class D {<br>  C c;<br>  :<br>  :<br>} d;<br><br></pre>
    <p style="font-style: italic;">An object of a class with a
constructor or destructor or a user-defined operator cannot be a member
of a union. So sayeth P182 of the ARM (1st sentence). Your BC 3.1
compiler should have given you the error &quot;Union member C::a is of type
class with constructor&quot;.</p>
  </li>
</ol>
PolonTang<br>
<br>
<p>Hi Mike,</p>
<p>I'm still reading through Overload 3 - good stuff! -- and I just saw
the comments about getting someone to write an ANSI-compliant string
class. Well, of course, Borland have! In fact, Pete Becker even
disappeared one day during the meeting &#8212; everyone assumed to implement
the subscript operators proposed by John Max Skaller (ISO Australia)
and adopted by the full committee. Also note that, since the article
was written, the class Size_T has been dropped (plain size_t to be used
instead), and a resize method has been added to match the
dynarray&lt;T&gt; template library class. I believe some minor changes
were made to the constructors too, but I don't have the info to hand
(sorry!).</p>
<p>Regards, Sean Corfield</p>
<p style="font-style: italic;">Thanks for the info Sean. Not everyone
will have access to a compiler that has an ANSI-compliant string class.
I hope that it can be implemented so that it can be used in conjunction
with compilers that do not support exceptions.</p>
<p>Since we last communicated I have received and read the offering of
&quot;Overload&quot;. I may say that I was pleasantly surprised with this since
there were several articles that were of interest to me. Maybe it's
because I have many years' C experience but am still learning the
vastly more complicated ways of C++.</p>
<p>I was particularly interested in article in Cheshire cats. It so
happens that I was contemplating the use of pointer classes for another
reason - automatic garbage collection. The problem with memory
management in C is that you can define pointers all over the place and
so no bit of data knows when nothing is depending on it, so the
programmer has to delete chunks of data explicitly when he things they
are no longer required. We all know that this is no always easy to work
out. C++ ought to do better, but it still allows pointers and so fails
miserably. Pointers are the problem, so if we ban them completely from
&quot;normal&quot; code, then there should be a method to keep track of data use
by the age-old method of reference counting:</p>
<pre>class Ref <br>{ <br>public:<br>  void ref() { if (this) count++; };<br>  void deref()<br>  {<br>    if (this &amp;&amp; --count == 0) <br>      delete this;<br>  }; <br>protected:<br>  Ref() { count = 0; };<br>  virtual ~Ref(){}; <br>private:<br>  int count; <br>};<br><br>class Ref_struct<br><br>class Ref_ptr<br>{<br>public:<br>  Ref_ptr(Ref_struct *p=0);<br>  Ref_ptr(Ref_ptr&nbsp; &amp;r) ;<br>  ~Ref_ptr();<br>  Ref_ptr&amp; operator =(Ref_ptr &amp;r)<br>  {<br>    point_at(r.ptr); <br>    return *this; <br>  };<br>  void New();<br>  Ref_struct *operator -&gt;() <br>  {<br>    return ptr; <br>  }; <br>protected:<br>  void point_at(Ref_struct *p); <br>  Ref_struct *ptr; <br>};<br><br>class Ref_struct : public Ref <br>{<br>  // The only thing that can create or delete<br>  friend void Ref_ptr::New();<br>  Ref_struct();<br>  ~Ref_struct(); <br>public:<br>  // The public interface <br>protected: <br>};<br><br>Ref_ptr::Ref_ptr(Ref_struct *p) <br>    : ptr(p)<br>{<br>  p-&gt;ref(); <br>}<br><br>Ref_ptr::Ref_ptr(Ref__ptr &amp;r)<br>    : ptr(r.ptr) <br>{<br>  ptr-&gt;ref(); <br>}<br><br>Ref_ptr::~Ref_ptr() <br>{<br>  ptr-&gt;deref();<br>}<br><br>void Ref_ptr::point_at(Ref_struct *p) <br>{<br>  // Must refer to the object in case<br>  //the deref accidently kills it<br>  p-&gt;ref();<br>  ptr-&gt;deref();<br>  ptr = p; <br>}<br><br>void Ref_ptr::New()<br>{<br>  ptr = new Ref_struct();<br>  ptr-&gt;ref(); <br>}</pre>
<p>The public interface of &quot;Ref_struct&quot; is available via the pointer
operator on a Ref_ptr, so to the user it looks just like a &quot;real&quot;
pointer. Pointer types need to be created for each structure type (so
that New() works). The pointer types could all be derived directly from
&quot;Ref_ptr&quot; or follow the hierarchy of the structures - it rather depends
on whether you want to add any additional functions to the pointer
types that might be usefully inherited.</p>
<p>One such use of additional functions is overloading of operators -
standard pointers have non-overloadable operations such as &quot;+&quot;. With
these defined pointer types &quot;+&quot; (or any other operator) could be
overloaded to be something more useful.</p>
<p>I don't think that these ideas are anything new, indeed Stroustrup
refers in passing to &quot;smart pointers&quot; that might update reference
counts. Nor do I guarantee that the above code it perfect - I ran a few
small tests and it seems to be OK.</p>
<p>One suggestion for layout of &quot;Overload&quot; - can you arrange for code
snippets to have the right number of characters in each line for it to
fit in a column? I think this is 59 or 60 chars. It is very confusing
having // comments wrapping round to the next line.</p>
<p>Thanks, Colin.</p>
<p style="font-style: italic;">In future I will take extra care with
wrapping comment lines. As you can see from your original text, the
comment lines were far too long for the column space available. I hope
this solution is to your taste. - Mike.</p>
</p>
<p><strong>Notes:</strong>&nbsp;</p>
<p><em>More fields may be available via dynamicdata ..</em></p>
</div>
</channel>
</rss>
