HP aC++/HP C A.06.25 Programmer's Guide
{
Vector operator + (Vector, Vector);
Vector v1(10), v2(10); // create two 10 element vectors
// details omitted
// v1 = v2 + 5; // Not legal - generates compile-
// time error
// Message: Illegal typEs
// associated with operator ‘+’:
// ‘Vector’ and ‘int’.
}
mutable Keyword
The mutable keyword is used in declarations of class members. It allows certain
members of constant objects to be modified in spite of the const of the containing object.
Usage
Often some class members are part of the implementation of the object, not part of the
actual information stored by the object. Although the information in the object needs
to stay unmodified in a const object, the implementation members may need to change.
These are declared mutable.
An example of this is use or reference count in an object that keeps track of the number
of pointers referring to it.
Example
class C {
public:
C();
int i;
mutable int j;
};
C::C() : i(1), j(3)
{
// Define constructor
}
void main()
{
const C c1;
C c2;
// c1.i =0; // Problem: compilation error
// Message: The left side of ‘=’
// must be a modifiable lvalue.
c1.j = 1; // OK
HP aC++ Keywords 187