Topic: const function and function prototype


Author: hstof@nki.nl
Date: 5 Nov 91 16:38:25 GMT
Raw View
Is it legal C++ to have a const as well as a "non-const" member function of the
same (overloaded) name?
An example of a simple vector class may make clear why this would be desirable:

Class elemvec_t is a resizable vector of objects of class elem_t. The
subscript operator is overloaded twice for the same class:
        - one version can be called for CONST objects of class elemvec_t
          and returns the VALUE of the element selected by argument ndx;
        - the other version can be called for VARIABLE objects of type elemvec_t
          and returns a REFERENCE to the element selected by argument ndx.
        (see lst. 1).
The "non-const" version enables the use of the subscript operator on the
lefthand side as well as the righthand side of an assignment whereas the
const version is restricted to righthand side (see lst. 2).


lst. 1: class elemvec_t
class elemvec_t {
        size_t  n;      // number of elements
        elem_t  *p;     // pointer to dynamically allocated array
        public :
        ...
        elem_t  operator[](size_t ndx) const    { return p[ndx]; }
        elem_t  &operator[](size_t ndx)         { return p[ndx]; }
        ...
};


lst. 2: using subscripting safely both for const and "non-const" vectors
void func(const elemvec_t &v, elemvec_t &w, elem_t e)
{

        size_t i;
        ...
        e = w[i];       // OK
        w[i] = e;       // OK
        e = v[i];       // OK, compiler uses const version
        v[i] = e;       // compiler detects error: v is const
        ...
}


On one C++ compiler (BCC) the above worked exactly as I intended it to work, on
another one it did not. Which compiler is wrong? Should the above code be
portable? In principle different member functions (including operator functions) can
have the same name as long as their function prototypes are clearly
distinguished. Ellis and Stroustrup [Ellis and Stroustrup, 1990, p. 308-309]
do give examples of the usage of the keywords const to create different
function prototypes for functions that carry the same name. However, in these
examples the keyword const is only used to specify "constness" of arguments.
In the above case (lst. 1) the keyword const is used differently, viz. to
specify that a particular function may be invoked for a const object of the
class. Should this use of the keyword const be considered part of the function
prototype and should the function prototype therefore be considered clearly
distinct from other functions that carry the same name?


HSTOF@NKI.NL
Huub Stoffers, Netherlands Cancer Institute




Author: ilanc@microsoft.com (Ilan CARON)
Date: 7 Nov 91 16:21:27 GMT
Raw View
In article <1991Nov5.163825.7273@nki.nl> hstof@nki.nl writes:
>Is it legal C++ to have a const as well as a "non-const" member function of the
>same (overloaded) name?
>[...]
>Should this use of the keyword const be considered part of the function
>prototype and should the function prototype therefore be considered clearly
>distinct from other functions that carry the same name?

[This cropped up on comp.lang.c++ as well recently]

The const modifier on a member function applies to the hidden THIS
pointer which is effectively part of the function's formal parameter
list.

Since applying const to a type introduces a new type, it is
the case that a const member function has a different ("clearly distinct")
prototype than the non-const version.

However, it appears that not all compilers out there get this right.

--ilan (ilanc@microsoft.com)