Const data member
You might want to have a const data member, some constant that
doesn't change after you instantiate the object.
An example of doing that is an Increment class with
the value that you would increment set by the constructor,
but not changing after its initialization.
main() {
Increment someValue(5);
...
}
class Increment {
public:
Increment(int = 1);
...
private:
int count;
const int incrementValue;
};
In the implementation of the constructor, you need to set
the data member incrementValue to the parameter passed in,
but by language definition, you are not allowed to assign
a value to a constant. The C++ language allows you to
set any data member in the following way.
Increment::Increment(int inc): incrementValue(inc) {
count = 0;
...
}
If you have more than one const data member, use constants to
separate the initializers:
: incrementValue(inc), anotherMember(blah) {
...
}