Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Related: “sending 'const NSString *' to parameter of type 'NSString *' discards qualifiers” warning

Sometimes, I think it's useful though. I may need to pass an a table and want to make sure that the content of the table does not change.

However, in most Objective-C program samples, I never see const. My question is why?

share|improve this question

2 Answers 2

up vote 15 down vote accepted

Due to the way that Objective-C objects work, const gives up being an enforcement and starts being a notation for the programmer. Consider this program:

int f(const int x) {
    return ++x;
}

int main(int argc, char *argv[]) {
    @autoreleasepool {
        int x = 3;
        NSLog(@"%d", f(x));
    }
    return 0;
}

That actually won't compile here (I'm using clang): the compiler can detect the attempt to modify the primitive C type and emits an error. But now compare it with this program:

NSMutableString *f2(const NSMutableString * const x) {
    [x appendString: @" world!"];
    return x;
}

int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSMutableString *x = [@"Hello" mutableCopy];
        NSLog(@"%@", f2(x));
    }
    return 0;
}

Even though the function is passed a constant pointer to a constant object, it's still possible to mutate the object.

In object-oriented programming, the best way to enforce the constant nature of an object is to make that object immutable - i.e. don't provide any methods that can change its state. Imagine that the function above took an NSString argument instead of NSMutableString, and that I'd passed the literal @"Hello" instead of a mutable copy. There is now, reasonably speaking, no chance of mutating the passed-in object[*]. Objective-C doesn't have any way of enforcing that though, unlike const or final object references in other OO languages.

For comparison, const works entirely differently in C++. If I get a const reference to a C++ object, I'm only allowed to call const member functions on that object. These functions preserve the const-ness of the object, either by not making any changes or by only modifying member variables that have explicitly been marked mutable by the class designer. So imagine that I had some type MutableString in C++ that's equivalent to NSMutableString in Objective-C. The equivalent of my example above would look something like:

MutableString& f3(const MutableString& x) {
  x.appendString(" world!");
  return x;
}

This definitely won't compile: in addition to appendString() not being a const operation, the function removes the const qualifier from the type reference which requires a const_cast.

[*]I expect there is some contorted way of doing it, but now we're into the realms of one programmer trying to sabotage another by doing "clever" things.

share|improve this answer
    
Thanks. So there is no way to prevent modification through const modifier. –  Jim Thio Jun 4 '12 at 10:26
    
Right. The compiler still treats const decoration as meaningful so you get contortions like aliases or casts to suppress warnings, but there's no change to the program's behaviour as a result of these contortions. –  user4051 Jun 4 '12 at 10:30

As far as Objective C goes- I don't program in it, so can't answer the question. The question I can answer though is "Should people use const a lot when programming in Objective C?" - That one a definite Yes.

Constness is essentially a software security feature that is used to make programs more reliable and maintainable. Many programmers grow up without really appriciating the value of consts, until one day something bad happens and they have an Ah Ha moment - "Oh -thats what const is used for". It's entirely possible to write a program with no constants, is virtually impossible to make such a program relaible.

Many samples of code are not really programs, just snippets that are designed to show a specific feature. They don't use Const as it detracts from the point the sample is trying to make. The other type of code you see is code written by inexperianced programmers who base what they do on the examples they have learnt from, or those that know about constants, and are too lazy to use them.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.