Reputation: 377
char* name;
const char* _name = "something";
name = _name; // conversion from const char * is not allowed
I know it is deprecated in c++, but I want to know why...
Why C++ banned name
to point some literals _name
points to?
Upvotes: 1
Views: 1331
Reputation: 52
Let's assume that name = _name was allowed.
Since they are both pointers, they will both be pointing to the same value.
That mean if we do name[0] ='B' that implies _name[0] = 'B' too (which is const) thats the reason why it's not allowed.
However, this is allowed
const char cc = 'a';
char c = 'b';
c = cc;
cc and c are NOT pointers so changing c afterward will not change cc
Upvotes: 0
Reputation: 64682
Because name
is non-const, it implies you are allowed to change the values.
For example:
*name = 'S'; // Change from "something" to "Something"
But _name
was declared const
, meaning you cannot change it.
You cannot take fixed, constant data, and assign it to a different variable; that is saying "It's OK if you change this".
Upvotes: 5
Reputation: 238351
I know it is deprecated in c++
It's not "deprecated" (and never has been). It's simply not allowed. The program is ill-formed. See 463035818_is_not_a_number's answer for something that used to be deprecated and is a bit similar.
Why is it not allowed
Pointers to const aren't implicitly convertible to pointers to non-const because of "const safety" (or "const correctness") which is a sub-concept of "type safety". In very short and abstract description: The type system of the language tries to prevent the programmer from making obvious mistakes.
If an object is const, then it cannot be modified. Attempting to modify a const object results in undefined behaviour (UB). UB is very, very bad. You must never modify a const object.
A pointer to const may point to a const object such as it does in the example. Since it isn't possible to modify the pointed object directly through it, this is fine. You can however modify objects directly through pointers to non-const. Thus, pointing to a const object with a pointer to non-const is unsafe. Since a pointer to const may point to a const object, converting a pointer to const into a pointer to non-const is unsafe. Hence, it is good that such conversion isn't implicit.
Upvotes: 2
Reputation: 122458
This:
name = _name; // conversion from const char * is not allowed
Was never allowed, because, sloppy speaking, it would turn something that is const
into something that can be modified. _name
is a pointer to const char
, but name
is a pointer to non-const char
.
What was allowed in the past is
char* _name = "something";
It was always deprecated in C++, compilers typically issued a warning for it. It has been removed in C++11 (see cppreference/string_literal), because it is lying about constness. Even though _name
is declared as char*
you cannot modify the string pointed to by it. The reason it was allowed was compatibility to C which initially did not have const
. It has been made an error, because trying to modify the string via _name
always was wrong. The type of "something"
is indeed const char[10]
.
Upvotes: 3
Reputation: 85351
const
in this context applies not the pointer, but to the value to which the pointer points.
const char*
is a (non-const) pointer to a const
char
.
char*
is a (non-const) pointer to a non-const char
.
These are different types. The non-const version allows modification of the pointed-to value, and the const one doesn't.
C++ type safety rules ensure const-correctness. A non-const type can be implicitly cast to a const type, but not the other way around. Stripping away const
requires an explicit cast (a const cast, which is often a code smell and/or a bug).
If you meant to have a const pointer itself, you should use char * const
. That's a const
pointer to a non-const char
. But note that a string literal like "something"
is always const
, it can't be modified.
Upvotes: 1
Reputation: 31379
What would the point of using const
be if it's to easy to circumvent? Rhetorical question.
It's not allowed for the simple fact that const
exists to prevent you from doing mistakes.
However, this is allowed:
const char *s = "hello";
char *t = (char*) s;
Would not recommend it though. It's still not allowed to modify a string literal through a non-const pointer.
Upvotes: 2