Re: Is C++ a type-safe language ??
From: John Harrison (john_andronicus_at_hotmail.com)
Date: Tue, 06 Jul 2004 08:18:21 +0100
> Thats what I was pointing out: Here the expression fails because the
> type of 'p' is const char * and 'pc' is char *....so the compiler
> gives 'cannot convert from const char * to char *'. But why doesn't
> give in the following case ?
> char *str = "String Literal";
> Here also type of "String Literal" is const char * and type of str is
> char * !!
No it isn't, the type of "String Literal" is const char , it's an
array not a pointer.
The conversion of this array or const char to char* is explicitly allowed
by 4.2 paragraph 2 of the standard. It's purpose is compatibility with C.
In the very old days C did not have const and so code like your example
Nevertheless it is an error to use this to modify a string literal.
One excemption from type safety rules for the purpose of bacwards
compatibility does not make C++ a non-type safe language. It is still more
type safe than any other language in common use (AFAIK).