Slimelite BBS

cchap.cpp

Back to EMULATE

From Chris Downs · 1996-04-04 16:32 · #73534 in EMULATE

 DM> Remember - I want this to emulate a cross between char
 DM> x[array_size] and char* px.  You cannot say 'x = "string"',
 DM> but you can say 'px = "string"'.  So if you say "px =
 DM> NULL", what are you saying?  That you want this pointer to
 DM> be NULL.

 What you want is for your class to return NULL when you want it
 to return the char* equivalent of a null cchar.  There's no need
 for your internal rep to mimic this same behavior.

 DM> As for using cchar x[0], I'll take that one.  Unfortunately, I'm not
 DM> using exceptions, otherwise I'd throw an error there.  :-/

 It might not be an error. In fact, this could be a representation of
 NULL! p = new char[0] is valid syntax.  Now only that, but then you can
 also do delete[] p when you are done.

 DM> That's one side of Pascal I like: ranges.

 Yes.  But run time support would be needed for this.  This runs
 counter to the "C culture" of being able to ignore constraints
 at run time since a fast program is much better than a correct
 program.

 DM> cchar& operator=(const char* psz) {
 DM>  if (pchar != NULL)
 DM> }

 CD>  This operator shouldn't be needed!  You've already
 CD> got a copy constructor
 CD>  which will automatically (ie implicitly) convert the
 CD> char* to a cchar.    If

 DM> IMO, the compiler shouldn't generate code for the conversion and then
 DM> throw    it over to the other operator= ... wouldn't it be slow to
 DM> assign a large string to a cchar then?  We would create the
 DM> temporary cchar (converting the char* to cchar) via strcpy,
 DM> and then change the current cchar to the new one by copying
 DM> again?  For small strings, this is only a nuisance, for

 I see your point.    But I would first make sure that this is a real
 bottleneck.  I know that compiler implementers are spending a lot
 of time at optimizing away temporary objects so it may not have to
 do the double copying.

 But if you do need the benefit of extra speed, just remember the
 drawback as well.     You are creating extra code that you don't
 "need" but you will have to maintain.  It's up to you to decide
 if the benefits of the "redunandant code" outweigh the cost.  I
 (as in me personally) wouldn't add that "redundant function" unless
 profiling showed this to be a bottleneck.    Then I would also make
 sure that I commented the function so that everybody knew I was
 adding in "redundant code" for a very specific purpose.  (BTW,
 Commenting the cchar& operator=(const cchar&) to note that this isn't
 used for char*'s implicitly converted would also be useful.)  Once
 compilers start supporting the explicit keyword, then some new
 considerations might apply.

 CD>  Also note that you need delete[] for your memory reclamation.

 DM> I heard, and it could very well be incorrect, that delete[]
 DM> was only required for arrays of objects where the
 DM> destructor had to be called for each object in the list,
 DM> although it wouldn't hurt to have it.

 If your new[] something as an array, then you need to delete[] it
 as an array.  While what you have may well work for built in types,
 the results (so I've read) aren't defined.



--- WILDMAIL!/WC v4.12 
 * Origin:   (1:100/4.0)