?
=?gb2312?B?wNbA1rTzzOzKpg==?=
i wrote:
i wrote:-----------------------------------------------------------------------
????? said:i wrote:
-----------------------------------------------------------------------
ä¹ä¹å¤§å¤©å¸ˆ said:i wrote:
-----------------------------------------------------------------------
Gianni said:The compiler is right. "unsigned char *" is non-const and string
literals (like "abcdg") are const.
Noah said:Gianni Mariani wrote: ....
It always intrigues me that such distinction is made in cases like this
yet this is perfectly ok from a legal point of view:
char * x = "I'm const";
Gianni said:The compiler is right. "unsigned char *" is non-const and string literals
(like "abcdg") are const.
If you're casting to a "unsigned char *" you need to ask yourself why.
Grizlyk said:or reinterpret_cast<>
unsigned char * p = reinterpret_cast<unsigned char *>(
const_cast<char*> ("abcdg") );
Grizlyk said:And i forget cast via "void*"
unsigned char * p = static_cast<void*> ("abcdg");
Kaz said:At this point you stop wrestling with the C++ bullshit and just use a
C style cast.
The good old C style cast will implicitly produce the correct
combination of reinterpret_cast and static_cast, without the verbiage
cluttering your code.
Since no typedef names are involved whose meanings could change, the C
style cast is no less safe than the new style cast combination, and is
much more readable.
(If you use the C cast with abstracted types, like casting some A * to
some B *, the danger is that someone may redefine what A and B is, and
the cast will silently continue to work, even though the change may be
such that the conversion has erroneous consequences).
Yes, sigh. I think the standard's commitee made a blunder on that one.
This is due to a rather short sighted view that there was a large legacy
code base that would break if that was not allowed. At a guess, I think
this would not be in the standard as an exception if hindsight was
available.
Michael said:Gianni Mariani a écrit :
The standard comitee didn't want to constraint compilers with putting
the array in a rw area.
The compiler is free to put in ro and save some copy time. The array can
even be shared among other part of the program.
That is a reason why casting away the consteness is undefined in this
case and should not be done.
Gianni Mariani a écrit :
The standard comitee didn't want to constraint compilers with putting
the array in a rw area.
The compiler is free to put in ro and save some copy time. The array can
even be shared among other part of the program.
That is a reason why casting away the consteness is undefined in this
case and should not be done.
What does this have to do with the choice to allow letting a pointer to
non-const point to string literals without a cast?
Casting away constness isn't undefined. Only writing to an object that was
initially declared const is.
Grizlyk said:I think, the following expression
unsigned char * p = static_cast<unsigned char *>(
const_cast<char*>
("abcdg") );
is not work because sizeof("type") can be not equal to
sizeof("unsigned type") in theory,
Andre said:So? That code is casting pointers-to-object. The pointer size won't
change, regardless of the size of the pointed to object.
Grizlyk said:I am not shure (i have no standard) that pointers for all types must
have fixed sizeof() (as "int" for example) or unsigned type must have
the same sizeof() as signed type.
For example there are memory models, where "sizeof(char *_far)==4",
but "sizeof(char *)==2".
Let pointers have fixed sizeof() and sizeof("type")!=sizeof("unsigned
type"). Here pointer arithmetic can be changed, because value of next
pointer (++ptr) is depending from size of object pointer point to.
Michael said:Rolf Magnus a écrit :
Why would you be allowed to cast it to not const if not to be able to
modify it ?
And if it is done automatically, there is the risk you modify the string
without remebering it is const.
[QUOTE=""Grizlyk said:The compiler is right. "unsigned char *" is non-const and string literals
(like "abcdg") are const.
If you're casting to a "unsigned char *" you need to ask yourself why.
Andre said:So? That code is casting pointers-to-object. The pointer size won't
change, regardless of the size of the pointed to object.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.