C-style casts allow you to cast away constness like in your example. In C++, you would normally use the new style casts such as static_cast which don't allow you to cast away constness. Only const_cast allows you to do that.
C-style casts allow you to cast away constness like in your example. In C++, you would normally use the new style casts such as static_cast, which don't allow you to cast away constness. Only const_cast allows you to do that.
It just stroke me today that – Artur Nov 16 '10 at 23:36 1 ..C compiler does not prevent you from writing to const variables and that const (apart from some obvious situations) does not help you much :-D – Artur Nov 16 '10 at 23:38 @Artur: Sure it does. When you attempt to modify a constant variable, you get a compiler error. It doesn't get more straightforward than that.
What it does let you do is explicitly cast types, so you can do stupid things all day long. But don't misplace that stupidity and say the C compiler or const isn't helpful; it's on you. – GMan Nov 16 '10 at 23:41 @GMan - I agree - I do not write such code on daily basis but today I've seen void pointer to const volatile as an argument to a routine and started to play with it and it turned out (was not really expecting that) that theoretically you can do anything with a memory area pointed to by that pointer and adding const will not make compiler to even warn you.
That is why I wrote that apart from some obvious situations (like dereferencing and assignment) your compiler will not try to do its best to stop you from writing to const area/variable. I did not expect compiler to just "ignore" it. – Artur Nov 17 '10 at 0:01 @Artur: Right.
Const is just a compile-time type annotation that is way less useful and flexible than type annotations present in many other languages. It's more or less useless, though plenty of people still use it as a sort of code-documentation thing. – Conrad Meyer Nov 17 '10 at 19:11.
Casting is your way of telling the compiler "I know what I'm doing", so it doesn't complain. Unfortunately, in this instance, you will invoke undefined behaviour.
9 Or, "unfortunately, in this instance, you don't know what you're doing" ;-) – André Caron Nov 16 '10 at 23:10.
To be equivalent, the 2nd line of the 2nd snippet int *ptr = ptr2const; // as expected error is raised here should be written as int *ptr = (int *)ptr2const.
Yes you are right – Artur Nov 16 '10 at 23:27.
Because C throws away a lot of type safety in order to gain a lot of speed instead. It cannot prevent you from doing incorrect things. It may try to warn you that you are doing incorrect things, but you can always work around the compiler if that is your goal.
Convert your constant to a string and you may find that while the compiler will let you cast away the const (inadvisable though it may be), the linker may put the constant string in read-only memory leading to a runtime crash.
This only "works" because the variable is local, and implementations have no way to enforce const-ness on a local (automatic) variable in general (at least not if the address of the variable is ever taken). But as far as the language specification is concerned, this is in the realm of undefined behavior. If you try this on a global/static variable, you'll quickly find that most implementations can and do enforce const by putting the variable in read-only memory which can be shared between multiple instances of your program.
C-style casts, such as (int*) are equivalent to C++ const_cast in their ability to cast away constness, so you can side-step const-correctness by using them, although such use is unrecommended (can lead to undefined behaviour). Int main() { const int x = 1; (int&)x = 2; std::cout On the other hand... void foo(const int& x) { (int&)x = 2; } int main() { int x = 1; foo(x); std::cout It's best just to use static_cast in most situations as this will warn you at compile time of any suspect behaviour in the form of a compile error.
1 They're not equivalent. This specific instance gives the same result as a const_cast. The reason for C++ having different cast syntaxes for casts of different nature is precisely to avoid accidentally invoking an invalid cast like this.
– André Caron Nov 16 '10 at 23:13.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.