|
From: Mac R. <re...@no...> - 2002-06-14 18:51:56
|
const and pointers gets tricky, because there are TWO things that could be
constant : the pointer itself, or the data that the pointer is pointing at.
In the code you found (const char * aPtr), the const refers to the data
that the pointer is pointing at. If, inside that function, you attempted
to say something like:
aPtr[0] = '\0';
The compiler should refuse it. Since technically the pointer is only
pointing at the first character, it would not *HAVE* to refuse
modifications to aPtr[1] or any later character, but I believe the C++
standard makes some special cases to handle arrays of chars because they
are so prevalent.
You can also have "char const * anotherPtr", which (if I haven't screwed
this up) is a constant pointer to non-constant data. That would mean that:
anotherPtr[0] = '\0';
would be legal, but:
anotherPtr = "";
would not.
Lastly, if you REALLY want everything to be constant, you can declare:
const char const * lockedPtrToLockedData = "Hello World";
Nothing about that can be modified without silly casting tricks.
To keep all this straight, try reading the declaration right-to-left:
const char * aPtr == (const char) (*) (aPtr)
"aPtr is a pointer to constant characters"
char const * bPtr == (char) (const *) (bPtr)
"bPtr is a constant pointer to characters"
const char const * cPtr == (const char) (const *) (cPtr)
"cPtr is a constant pointer to constant characters"
The really unfortunate thing about this is that you have to read the
declaration right to left, but when you see "const" you have to associate
it with the thing to its right. That always makes me feel like I'm having
to flip between reading right-to-left and reading left-to-right.
That ends the useful help section of this posting. Now for the whiny "I
wish they'd done it differently part":
I really wish that C had used the Pascal or (modern) BASIC declaration style:
Pascal:
MyInteger : INTEGER;
BASIC:
Dim MyInteger As Integer
These declarations are read left to right, and can easily be extended for
consts and pointers. Wouldn't the following have been easier to read?
aPtr : * const char; // pointer to constant char
bPtr : const * char; // constant pointer to char
cPtr : const * const char; // constant pointer to constant char
or, while I'm dreaming up changes that will never happen -- it has always
bothered me that "char *" was a pointer, but to get a pointer from a
variable, you used "&variable". From that standpoint, I would prefer &
instead of * for declaration, resulting in:
aPtr : & const char;
etc.
(Of course, you'd have to come up with a different character for references
in C++) I also think that @ would be a better dereferencing operator than
*, because you are trying to access the value "at" an address. But it's
far too late for these things now, so they will just remain in my wishful
thinking category...
Mac
At 10:42 PM 6/14/02 +0530, Abhijit Shylanath wrote:
>I was just going through the sources of the class libraries in Turbo C++
>version 3.0, and found this code. How does the program modify aPtr even
>though it says it's a const?
>
>-------CLASSLIB/SOURCE/STRNG.CPP------
>String::String( const char *aPtr )
>{
> if ( aPtr == 0 )
> aPtr = "";
>
> len = strlen( aPtr ) + 1;
> theString = new char[ len ];
> CHECK( theString != 0 );
> strcpy( theString, aPtr );
>}
-
_____________________________ /"\
Mac Reiter \ / ASCII Ribbon Campaign
Nomadics, Inc. X Against HTML Mail
re...@no... / \ (To join the campaign, simply use
this in your signature.)
|