Slimelite BBS

Re: cchar.hpp

Back to EMULATE

From Chris Downs · 1996-04-07 18:08 · #73644 in EMULATE

In reply to cchar.hpp by Darin McBride

 CD>> Why should the  value of pchar matter once the object is 
 CD>> deleted?  The only reason it  should matter is if you made a 
 CD>> boo-boo and tried to delete the thing more than once.
 
 EB>> ??  No, that's the only time that setting it to zero would
 EB>> mask the bug (and it isn't necessarily a bug to attempt to
 EB>> delete it more than once, IMHO).
 
 CD>  Well, we disagree there.  If it wasn't a "bug" to delete an 
 CD> already  deleted object  or in anyway access an already 
 CD> deleted item, then  this imposes some extra restrictions on 
 CD> all your class implementations.  

 EB> We're not talking about deleting an already deleted *object*.  I was
 EB> referring to an already deleted private *member* of an object in which
 EB> we may very reasonably use a NULL pointer meaning "not yet allocated."

 But that's not what it means when NULL is reset _in_the_destructor_.
 Then the meaning of NULL becomes even more overloaded.  Now I
 certainly can understand the possible need for flagging illegal uses
 of an object, particularly for debug builds.  But if I was going to
 try and track down uses of an object after it's been deleted, (and
 that can be something nasty to track), then I would probably opt
 for some private boolean variable just for this purpose.  #ifdef'ing
 it in and out would make it an easy tool to use.  Note that resetting
 the rep _in_the_destructor_ really tells us little.  Is it because we
 did something illegal or is it because the class implementation
 doesn't fully support all the legal representations?

 CD>  But I can see your point that it could be a bug spotter if the 
 CD> only  illegal memory access your system can trap is 
 CD> dereferencing a NULL  pointer.  But again, your class 
 CD> implentation must be set up so  that a NULL internal rep is invalid.  

 EB> That doesn't follow.  It may be valid data, but not for all
 EB> operations.

 As I told Darin, then it makes sense for the member functions that
 should know about what's valid and what isn't to do what it should be
 in a better position compared to the client code.

 Since operator[] or operator() are in the best position
 to know that something dramatically wrong is going on, that's the
 place to take action.  If nothing else, I would rather have the
 class implementation either take action or at least notify me of
 impending doom.  And if I were implementing a class and I knew that
 valid uses caused errors, I sure wouldn't want the program to crash
 and burn at some arbitrary place of client code when I have control
 over something so critical.

 EB> For example, it's certainly allowable to have an ifstream
 EB> without yet having opened any file.  Using, say, the seekg() member
 EB> function on an unopened ifstream will result in an error.

 My point exactly.  The call to seekg() will tell me that things are
 wrong at the earliest possible moment.

 EB> Similarly,
 EB> NULL might be a valid internal representation for an unallocated string
 EB> even though this will cause some operations to fail.  Now how we decide
 EB> to *handle* these errors (or not!) is where we get into the difference
 EB> between a full blown string class and something like cchar.

 I guess we agree sort of.  I'm not sure what the disadvantages to
 the "full blown" classes might be?  It's not like bulletproofing
 members with more code will make each object any bigger.
 If sluggishness is a concern, then I would have to question the
 wisdom of using a cchar class in the first place.
 
 EB> It's fortunate that neither of us
 EB> ever make such mistakes any more.  ;-)

 That's my goal Ed.  I don't want to make the same mistake more than
 once!

---
 þ Blue Wave/QWK v2.12 þ

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