|
From: Juliano <jp...@bo...> - 2002-06-14 19:11:08
|
I'm sorry if I'm wrong, but... Isn't "const char *y" a way to say that the address value stored in the pointer "y" can't be changed? In other words, I can change the value pointed by the address stored in "y", but I can't change the address stored in "y" itself. Isn't it the way pointers work? Am I doing something wrong? []s Juliano -----Original Message----- From: dev...@li... [mailto:dev...@li...] On Behalf Of Mac Reiter Sent: sexta-feira, 14 de junho de 2002 15:50 To: Dev-C++ Mailing List Subject: Re: [Dev-C++] const modifier? Quer ter seu pr=F3prio endere=E7o na Internet? Garanta j=E1 o seu e ainda ganhe cinco e-mails personalizados. = Dom=EDniosBOL - http://dominios.bol.com.br 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] =3D '\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] =3D '\0'; would be legal, but: anotherPtr =3D ""; would not. Lastly, if you REALLY want everything to be constant, you can declare: const char const * lockedPtrToLockedData =3D "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 =3D=3D (const char) (*) (aPtr) "aPtr is a pointer to constant characters" char const * bPtr =3D=3D (char) (const *) (bPtr) "bPtr is a constant pointer to characters" const char const * cPtr =3D=3D (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=20 >C++ version 3.0, and found this code. How does the program modify aPtr=20 >even though it says it's a const? > >-------CLASSLIB/SOURCE/STRNG.CPP------ >String::String( const char *aPtr ) >{ > if ( aPtr =3D=3D 0 ) > aPtr =3D ""; > > len =3D strlen( aPtr ) + 1; > theString =3D new char[ len ]; > CHECK( theString !=3D 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.) =20 _______________________________________________________________ Don't miss the 2002 Sprint PCS Application Developer's Conference August 25-28 in Las Vegas - http://devcon.sprintpcs.com/adp/index.cfm?source=3Dosdntextlink _______________________________________________ Dev-cpp-users mailing list Dev...@li... https://lists.sourceforge.net/lists/listinfo/dev-cpp-users |