Re: OON: const and aliasing

From: Jonathan Coxhead (jonathan@doves.demon.co.uk)
Date: Fri Mar 24 2000 - 13:34:35 EST


   Todd wrote,

 | const Vector& acts as if it is:
 |
 | class Vector {
 | public:
 | double const* z; // Cannot change z
 | };
 |
 | instead of
 |
 | class Vector {
 | public:
 | const double* z; // Cannot change z[i]
 | };

   This is wrong! In

      class Vector {
      public:
         double const *z;
      };

you cannot change |z[i]|. It means *exactly the same* as

      class Vector {
      public:
         const double* z; // Cannot change z[i]
      };

   The other possibility is

      class Vector {
      public:
         double *const z;
      };

where you cannot change |z|.

   This is much clearer if you always write qualifiers on the right
of the type name. Then, if you think of the qualifier as a unary
operator, its argument is the thing that is const (i e, a non-
modifiable lvalue):

      double const *z; //|*z| is const
      double *const z; //|z| is const

and there's also

      double const *const z; //|z| and |*z| are both const

   (This way of looking at it doesn't work so well for constant
references, but I'd still prefer to write |Vector const &v| for a
const reference argument.)

 | On the other hand, if we had defined Vector as:
 |
 | class Vector {
 | public:
 | double z[3];
 | };
 |
 | then the elements of a const Vector& WOULD be const.

   Right. The qualifier propagates to array members.

        /|
 o o o (_|/
        /|
       (_/

--------------------- Object Oriented Numerics List --------------------------
* To subscribe/unsubscribe: use the handy web form at
http://oonumerics.org/oon/
* If this doesn't work, please send a note to owner-oon-list@oonumerics.org



This archive was generated by hypermail 2b29 : Wed Feb 20 2002 - 03:20:11 EST