Re: why does const not work on pointed objects?
Corno wrote:[color=blue][color=green]
>>I assume the OP is referring to something like the following:
>>
>>#include <string>
>>
>>struct foo
>>{
>> void bar() const
>> {
>> data = "hello"; // not allowed
>> }
>> std::string data;
>>};
>>
>>struct foo2
>>{
>> void bar() const
>> {
>> *data = "hello"; // allowed
>> }
>> std::string* data;
>>};
>>
>>In which case, I assume the OP's point is that the fact that 'data' is
>>held via a pointer makes it no less part of the object's data, so why
>>should you get non-const access to it from a const member function?[/color]
>
>
> so the value of using const in a public member function of a class (it's
> interface) is changed for me.[/color]
Then you had it wrong all along. The language never guaranteed that
const objects wouldn't change. The "const" keyword is mostly a hint
from one programmer to another that an object may be treated as though
it will not change. The language does not enforce const-ness.
[color=blue]
> It's no guarantee to the user that nothing will change; it imposes a IMHO
> strange restriction on the implementation; a part of the related data of an
> instance, eg, only the direct member data is not allowed to change. But as
> the example above shows, the same behaviour of the class can be implemented
> in another way (which is not a hack to me) that is not hindered by const.
> The only thing it does is that it creates 2 interfaces to the same class,[/color]
They're not the same class.
[color=blue]
> it
> makes a distinction between all users into 2 groups; users that have full
> access to all the member functions and users that can only use the const
> functions (either or not really const).[/color]
Nonsense. The functionality you want is easily implemented per Dylan's
excellent suggestion.
If it didn't sound so stupid, I'd pull out one of those platitudes about
"the spirit of the language." ;)
Corno wrote:[color=blue][color=green]
>>I assume the OP is referring to something like the following:
>>
>>#include <string>
>>
>>struct foo
>>{
>> void bar() const
>> {
>> data = "hello"; // not allowed
>> }
>> std::string data;
>>};
>>
>>struct foo2
>>{
>> void bar() const
>> {
>> *data = "hello"; // allowed
>> }
>> std::string* data;
>>};
>>
>>In which case, I assume the OP's point is that the fact that 'data' is
>>held via a pointer makes it no less part of the object's data, so why
>>should you get non-const access to it from a const member function?[/color]
>
>
> so the value of using const in a public member function of a class (it's
> interface) is changed for me.[/color]
Then you had it wrong all along. The language never guaranteed that
const objects wouldn't change. The "const" keyword is mostly a hint
from one programmer to another that an object may be treated as though
it will not change. The language does not enforce const-ness.
[color=blue]
> It's no guarantee to the user that nothing will change; it imposes a IMHO
> strange restriction on the implementation; a part of the related data of an
> instance, eg, only the direct member data is not allowed to change. But as
> the example above shows, the same behaviour of the class can be implemented
> in another way (which is not a hack to me) that is not hindered by const.
> The only thing it does is that it creates 2 interfaces to the same class,[/color]
They're not the same class.
[color=blue]
> it
> makes a distinction between all users into 2 groups; users that have full
> access to all the member functions and users that can only use the const
> functions (either or not really const).[/color]
Nonsense. The functionality you want is easily implemented per Dylan's
excellent suggestion.
If it didn't sound so stupid, I'd pull out one of those platitudes about
"the spirit of the language." ;)
Comment