14.2 Constant Functions
C++
lets you define two types of numbers: constant and nonconstant. For
example:
int index; // Current index into the data array
const int DATA_MAX(100); // Maximum number of items in the array
These two items are treated differently. For example, you can change
the value of index, but you can't
change DATA_MAX.
Now let's consider a class to implement a set of
numbers from 0 to 31. The definition of this class is:
// Warning: The member functions in this class are incomplete
// See below for a better definition of this class
class int_set {
private:
// ... whatever
public:
int_set( ); // Default constructor
int_set(const int_set& old_set); // Copy constructor
void set(int value); // Set a value
void clear(int value); // Clear an element
int test(int value); // See whether an element is set
};
As with numbers, C++ will let you define two types of
int_set objects: constant and nonconstant:
int_set var_set; // A variable set (we can change this)
var_set.set(1); // Set an element in the set
// Define a constant version of the set (we cannot change this)
const int_set const_set(var_set);
In the int_set class, there are member functions
such as set and clear that
change the value of the set. There is also a function
test that changes nothing.
Obviously you don't want to allow
set and clear to be used on a
constant. However, it is okay to use the test
member function.
But how does C++ know what can be used on a constant and what
can't? The trick is to put the keyword
const at the end of
the function header. This tells C++ that this member function can be
used for a constant variable. So if you put const
after the member function
test, C++ will allow it to be used in a constant.
The member functions set and
clear do not have this keyword, so they
can't be used in a constant.
class int_set {
private:
// ... whatever
public:
int_set( ); // Default constructor
int_set(const int_set& old_set); // Copy constructor
void set(int value); // Set a value
void clear(int value); // Clear an element
int test(int value) const; // See whether an element is set
};
Thus, in your code you can do the following:
int_set var_set; // A variable set (we can change this)
var_set.set(1); // Set an element in the set (legal)
// Define a constant version of the set (we cannot change this)
const int_set const_set(var_set);
// In the next statement we use the member function "test" legally
std::cout << "Testing element 1. Value=" << const_set.test( ) << '\n';
However, you cannot do the following:
const_set.set(5); // Illegal (set is not allowed on a const)
The member function set was not declared
const, so it cannot be invoked on a const
int_set object.
|