4.7 Constant Methods, Parameters, and Variables




Methods, variables and parameters can be declared so that their ability to cause change or be changed is restricted. A method can be limited so that it does not change the object on which it is operating. A parameter can be limited so that it cannot be changed by the method to which it is passed. A variable can be limited so that it has a constant, unchanging value.

Limiting methods, variables and parameters in this way is useful for three reasons. First, the additional declaration provides a more specific statement of the intention of the programmer. Often these intentions cannot be expressed and are, perhaps inadvertantly, violated, causing unintended behavior and perhaps system errors. With the additional declaration, the compiler is able to detect and prevent unintended usage. Second, when errors do occur, the additional declarations help to reduce the area of the system that must be examined. If data has been improperly changed, the cause cannot directly be in those areas declared not to cause or allow such changes. Third, the additional information often helps the compiler to optimize better the executable code. For example, if the compiler knows that a variable cannot change, it need never generate code that would save and restore its value.

The keyword "const" is used as a modifier on the declarations of methods, variables and parameters. The Location class below shows how the "const" modifier is used on methods.

  class Location {                         // Version 1
   private:

     int  currentX, currentY;

   public:

        Location(int x, int y);         // specific location
        Location();                     // default location
     int Xcoord() const;                // return x-axis coordinate
     int Ycoord() const;                // return y-axis coordinate
   };

   // the implementation follows

    Location::Location( int x, int y ) { currentX = x; currentY = y; }

    Location::Location () { currentX = -1; currentY = -1; }

int Location::Xcoord() const { return currentX; }

int Location::Ycoord() const { return currentY; }

In this code the "const" modifier is used to declare that the two methods Xcoord() and Ycoord() do not change the Location object on which they operate. These two methods are typical of "const" methods: they return information about the object without changing the object itself. Methods of this kind simply "query" the object without altering it.

Note in the above example that the "const" modifier must appear both where the method appears in the class definition and again in the implementation part where the method's implementation is given.

An extended version of the Location class is shown below to illustrate how "const" is used with a parameter. In this extended definition a new method has been added that tests whether the screen coordinates defined by a Location object lies inside the boudaries of a given Frame object. The Frame object is passed by reference (to avoid the expense of copying the Frame object) and it is declared "const" because we do not need to modify the Frame object to determine whether a point lies in the Frame.

  class Location {                         // Version 2
   private:

     int  currentX, currentY;

   public:

        Location(int x, int y);         // specific location
        Location();                     // default location
     int Xcoord() const;                // return x-axis coordinate
     int Ycoord() const;                // return y-axis coordinate
     int Inside(const Frame& frame );   // if location is inside frame
   };

   // the implementation follows

    Location::Location( int x, int y ) { currentX = x; currentY = y; }

    Location::Location () { currentX = -1; currentY = -1; }

int Location::Xcoord() const { return currentX; }

int Location::Ycoord() const { return currentY; }

int Location::Inside (const Frame& frame) {
   Location frameAt    = frame.WhereAt();
   Shape    frameShape = frame.WhatShape();

   if (     frameAt.Xcoord() <= currentX                     
         && currentX <= frameAt.Xcoord() + frameShape.Width()
         && frameAt.Ycoord() <= currentY                      
         && currentY <= frameAt.Ycoord() + frameShape.Height()   )

        return 1;

   else return 0;
 }

Notice again that the "const" modifier for the parameter appears both in the class definition and in the implementation where the code for the method is given.

Variables can also be declared with a "const" attribute. In some cases these are global variables that are a better alternative to #define preprocessor macros. Some examples of constant variables are:


	const int VersionNumber = 1;
        const int ReleaseNumber = 5;
        const double Pi = 2.141598;

Variables declared "const" must be given an initial value when they are are declared as there is no othe way to provide a value for the variable - since it is "const" it cannot be altered.


Next Stop


Last Updated: August 22, 1995 / kafura@cs.vt.edu