From Chris Downs · 1996-04-04 16:32 · #73533 in EMULATE
DM> operator char*() const {
DM> return pchar;
DM> }
DM> operator const char*() const {
DM> return pchar;
DM> }
CD> You should only need the const char* version above.
DM> How about if the user wants to do something like:
DM> char* p = cchar_obj;
DM> while (*p++) {
DM> //do stuff
DM> }
DM> I just allow for that possibility. If you can show me that
DM> it still isn't needed, I'm yanking it.
Oops, I think I made a mistake. I think you do need both operator()
deals, but not for the reason you've got above. You can do
while (*p++) for both char*'s and const char*'s. A const char*
can't change what it is pointing to. But the value of the pointer
itself can change and you certainly have read access thru the pointer.
But as Jerry said, leaving those internal reps open to whatever
looks mighty dangerous.
Since your motivation for the operator()'s is to "just allow for
the possibility", I would yank both and see what happens. If you
don't need them, then you don't need them. Perhaps adding a
member function that returns the string's buffer would make
more sense so that then char* p = cchar1.GetBuffer() would do the
same thing. Speed considerations wouldn't apply since returning the
rep pointer is a natural for inlining. It seems to me that a
GetBuffer() type of approach would be a safer way to fly.
CD> Remember how you implemented the representation!! pchar
CD> could legally be NULL!!
DM> Yes! And so can a 'char*'!
Right. So if you are going to allow the rep to be NULL, all the
member functions should understand that this is legal. If you
are going to permit a NULL rep, then you need to allow for it
for your operator[] and other ones as well. And if you want it
to crash for operator[] on a NULL rep, I think it would be a
better idea to do your abort() in operator[] rather than hoping
the abort() shows up at some arbitrary location of client code.
--- WILDMAIL!/WC v4.12
* Origin: (1:100/4.0)