When you're designing C programs for embedded systems, or special purpose programs that need to refer to the same memory (multi-processor applications sharing memory) then you need constant pointers.
For instance, I have a 32 bit MIPs processor that has a little LCD attached to it. I have to write my LCD data to a specific port in memory, which then gets sent to the LCD controller.
I could #define that number, but then I also have to cast it as a pointer, and the C compiler doesn't have as many options when I do that.
Further, I might need it to be volatile, which can also be cast, but it's easier and clearer to use the syntax provided - a const pointer to a volatile memory location.
For PC programs, an example would be: If you design DOS VGA games (there are tutorials online which are fun to go through to learn basic low level graphics) then you need to write to the VGA memory, which might be referenced as an offset from a const pointer.
Trying to collect some uses:
Binding some temporary to reference-to-const, to lengthen its lifetime. The reference can be a base - and the destructor of it doesn't need to be virtual - the right destructor is still called:
ScopeGuard const& guard = MakeGuard(&cleanUpFunction);
Explanation, using code:
struct ScopeGuard {
~ScopeGuard() { } // not virtual
};
template<typename T> struct Derived : ScopeGuard {
T t;
Derived(T t):t(t) { }
~Derived() {
t(); // call function
}
};
template<typename T> Derived<T> MakeGuard(T t) { return Derived<T>(t); }
This trick is used in Alexandrescu's ScopeGuard utility class. Once the temporary goes out of scope, the destructor of Derived is called correctly. The above code misses some small details, but that's the big deal with it.
Use const to tell others methods won't change the logical state of this object.
struct SmartPtr {
int getCopies() const { return mCopiesMade; }
};
Use const for copy-on-write classes, to make the compiler help you to decide when and when not you need to copy.
struct MyString {
char * getData() { /* copy: caller might write */ return mData; }
char const* getData() const { return mData; }
};
Explanation: You might want to share data when you copy something as long as the data of the originally and the copie'd object remain the same. Once one of the object changes data, you however need now two versions: One for the original, and one for the copy. That is, you copy on a write to either object, so that they now both have their own version.
Using code:
int main() {
string const a = "1234";
string const b = a;
// outputs the same address for COW strings
cout << (void*)&a[0] << ", " << (void*)&b[0];
}
The above snippet prints the same address on my GCC, because the used C++ library implements a copy-on-write std::string
. Both strings, even though they are distinct objects, share the same memory for their string data. Making b
non-const will prefer the non-const version of the operator[]
and GCC will create a copy of the backing memory buffer, because we could change it and it must not affect the data of a
!
int main() {
string const a = "1234";
string b = a;
// outputs different addresses!
cout << (void*)&a[0] << ", " << (void*)&b[0];
}
For the copy-constructor to make copies from const objects and temporaries:
struct MyClass {
MyClass(MyClass const& that) { /* make copy of that */ }
};
For making constants that trivially can't change
double const PI = 3.1415;
For passing arbitrary objects by reference instead of by value - to prevent possibly expensive or impossible by-value passing
void PrintIt(Object const& obj) {
// ...
}
Best Answer
const
is a tool which you should use in pursuit of a very important C++ concept:Even though it doesn't change the functionality, adding
const
generates a compiler error when you're doing things you didn't mean to do. Imagine the following typo:If you use
int* const
, this would generate a compiler error because you're changing the value toptr
. Adding restrictions via syntax is a good thing in general. Just don't take it too far -- the example you gave is a case where most people don't bother usingconst
.