From Darin McBride · 1996-04-06 06:56 · #73627 in EMULATE
In reply to cchar.cpp by Chris Downs
DM> operator char*() const {
DM> operator const char*() const {
CD> Since your motivation for the operator()'s is to "just allow for
CD> the possibility", I would yank both and see what happens. If you
The motivation for the 'second' operator <> char*() is to just allow for the possibility. The reason for having them is so that the cchar will work as a normal char*.
cchar pch = "Some stuff,and more stuff";
char* pcomma = strchr(pch,',');
I didn't want to use it as strchr(pch.GetBuffer(),',')...
CD> don't need them, then you don't need them. Perhaps adding a
CD> member function that returns the string's buffer would make
CD> more sense so that then char* p = cchar1.GetBuffer() would do the
CD> same thing. Speed considerations wouldn't apply since returning the
CD> rep pointer is a natural for inlining. It seems to me that a
CD> GetBuffer() type of approach would be a safer way to fly.
It would be - if I wanted the safer class, I fully admit. And when I'm going for full-blown classes, (or at least almost-full-blown) I do it that way. GENERALLY, I want to hide how the class implements things. IN THIS CASE the only thing I wanted hidden was that this array of characters was on the heap rather than on the stack. And then I found that it could make things like strdup easier, thus the operator=.
CD> Remember how you implemented the representation!! pchar
CD> could legally be NULL!!
DM> Yes! And so can a 'char*'!
CD> Right. So if you are going to allow the rep to be NULL, all the
CD> member functions should understand that this is legal. If you
It's legal, but dereferencing it is just as 'legal' as dereferencing ((char*)NULL)[4]...
CD> are going to permit a NULL rep, then you need to allow for it
CD> for your operator[] and other ones as well. And if you want it
CD> to crash for operator[] on a NULL rep, I think it would be a
CD> better idea to do your abort() in operator[] rather than hoping
CD> the abort() shows up at some arbitrary location of client code.
True - this could provide me with better checks. Actually, I would rather simply assert that pchar != NULL. This would then remove itself in the release version. :-)
Thanks, I'll take that. :-)
--- Maximus/2 3.01
* Origin: Tanktalus' Tower BBS (PVT) (1:342/708)