I have this code example:
const NSMutableString *const foobar = [[NSMutableString alloc] initWithFormat:@"Hello"];
[foobar appendString:@" World"];
NSLog(@"String: %@", foobar);
and outputs:
String: Hello World
Should my variable be const(as well as a pointer)? Therefore, I could not modify it.
C ++ behaves the way I expect
int main() {
const std::string *const s = new std::string("hello");
s->append(" world");
std::cout << s << std::endl;
}
output:
$ g++ test.c++
test.c++: In function ‘int main()’:
test.c++:7:20: error: passing ‘const string {aka const std::basic_string<char>}’ as ‘this’ argument of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::append(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ discards qualifiers [-fpermissive]
s->append(" world");
I know that I should not use constmutable, but that does not change the fact that mutable should be const.
source
share