[comp.lang.c++] Overloaded a->b ==

gintera@cs-sun-fsa.cpsc.ucalgary.ca (Andrew Ginter) (03/26/91)

In section 13.4, ARM suggests:

  "For example, if more than one of the operators ->, *, and []
   are defined for a class V, one would expect v->m, (*v).m, and 
   v[-].m to have identical values for all objects v of class V."

At the same time, we find in section 13.4.6:

  "An expression x->m is interpreted as (x.operator->())->m for
   a class object x.  It follows that operator->() must either
   return a pointer to a class or an object of or a reference to
   a class for which operator->() is defined."

If operator-> returns a reference to a class for which operator -> is
defined, the processes of resolving the overloaded operator ->
continues recursively until something other than an object of or a
reference to a class with an operator-> is returned.  

This same is NOT true of an overloaded operator*().  It may therefore
be non-trivial for a programmer to maintain the a->b == (*a).b
equivalence.

Have I missed something here?

Andrew Ginter, 403-220-6320, gintera@cpsc.ucalgary.ca

wmm@world.std.com (William M Miller) (03/27/91)

gintera@cs-sun-fsa.cpsc.ucalgary.ca (Andrew Ginter) writes:
> In section 13.4, ARM suggests:
>
>   "For example, if more than one of the operators ->, *, and []
>    are defined for a class V, one would expect v->m, (*v).m, and
>    v[-].m to have identical values for all objects v of class V."
>
> At the same time, we find in section 13.4.6:
>
>   "An expression x->m is interpreted as (x.operator->())->m for
>    a class object x.  It follows that operator->() must either
>    return a pointer to a class or an object of or a reference to
>    a class for which operator->() is defined."
>
> If operator-> returns a reference to a class for which operator -> is
> defined, the processes of resolving the overloaded operator ->
> continues recursively until something other than an object of or a
> reference to a class with an operator-> is returned.
>
> This same is NOT true of an overloaded operator*().  It may therefore
> be non-trivial for a programmer to maintain the a->b == (*a).b
> equivalence.
>
> Have I missed something here?

I don't know.  The lesson to be learned here, I think, is that having
operator->() return something other than a pointer to a class is a feature
that should usually be avoided, especially if there is also an operator*()
defined.  I'm sure there must be times when it's useful to be able to return
an object of a class with operator->() defined instead of a pointer to a
class, but I've never had need to do so.

-- William M. Miller, Glockenspiel, Ltd.
   wmm@world.std.com