Re: const StringPtr != ConstStringPtr
Re: const StringPtr != ConstStringPtr
- Subject: Re: const StringPtr != ConstStringPtr
- From: David Fang <email@hidden>
- Date: Tue, 10 Jan 2006 14:23:03 -0500 (EST)
> During the conversion of our projects from CW to Xcode, I got errors
> on all calls that passed a constant string to functions that take a
> const StringPtr. If ConstStringPtr is a typedef of const unsigned
> char* and StringPtr is a typedef of unsigned char*, then why does
> Xcode (or gcc rather) think they're different?
>
> void Func(const StringPtr s);
>
> Func("\pblah"); <--error
If you expand the typedefs, you'll see why they are not the same:
StringPtr is a pointer to char
ConstStringPtr is a pointer to const char,
the pointer itself is mutable, but the pointee (char) is
read-only.
const StringPtr is a const pointer to char,
that is, the pointer is const (immutable) while the pointee (char)
remains modifiable.
const ConstStringPtr is a const pointer to const char
In your example, Func expects a const pointer to a modifiable string,
however, you pass it a const char[], which is a non-writable string,
hence the error.
constness was not always respected in C type-checking, but is always
checked in C++ type-checking. This might explain why another compiler (in
C mode) may not have caught the constness error before.
HTH.
David Fang
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden