Topic: overloading operator ->*
Author: "Dodgson, David S" <David.Dodgson@unisys.com>
Date: 1996/09/30 Raw View
Michael C. Greenspon <mcg@wheezy.CS.Berkeley.EDU> wrote in article
<mcg-2609961308210001@itchy.cs.berkeley.edu>...
> Could someone please definitively explain the complete syntax &
semantics
> of overloading operator->*(), both as a member and a global function?
>
> Neither ARM nor the draft std (April, '95-- where could I find a more
> recent version?) give much of a clue. The xlC compiler seems to
require
at
> least one of the args to be a class or enum, but shouldn't ->* be
> applicable to a left arg of pointer to object type, and perhaps an
object
> as an extension? (cf. operator-> which when overloaded applies to an
> object as left arg not ptr to object.) Metrowerks uniformly chokes on
what
> xlC accepts. I have some hunches but I'd like to hear it from someone
who
> actually knows.
>
operator->* is not like operator->. While there are special rules for
handling operator->, there are no special rules for operator->*. In
other
words, operator->* is a binary operator just like, say, operator/.
You overload operator->* by providing the two parameters for the
function,
the left and right operands of the binary operation.
If you want to use it for generic pointer
operations like operator-> (in, for example, a smart pointer class) you
must use templates. You can define a template which takes parameters
of
smart_pointer<T> for the left operand and pointer-to-member-of-T of
type U
for the right operand. Something like: (I haven't tried this :-)
template <class T, class U>
U operator->*(smart_pointer<T> p, U T::* mem) {
return ((*p).*mem); }
Unfortunately, if you want pointer-to-member-function then things
really
get complicated. That requires a series of templates, helper classes,
and partial specialization, depending on the number of
parameters of the p-t-m function.
The net result of this complexity is that you don't see standard
library
classes like auto_ptr and iterators do anything with ->*.
You may be better off not trying
to overload this operator and instead requiring (*smart_p).*ptm
wherever you want to use pointer-to-members.
FYI, a proposal to change the behavior of operator->* to act like
operator-> was voted down by the standards committee in March.
-------------------------------------------------------------------------
------------------------------------------------------------
David Dodgson Core Business
"It's not just a job!"
Unisys Corp. dsd@tr.unisys.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: mcg@wheezy.CS.Berkeley.EDU (Michael C. Greenspon)
Date: 1996/09/26 Raw View
Could someone please definitively explain the complete syntax & semantics
of overloading operator->*(), both as a member and a global function?
Neither ARM nor the draft std (April, '95-- where could I find a more
recent version?) give much of a clue. The xlC compiler seems to require at
least one of the args to be a class or enum, but shouldn't ->* be
applicable to a left arg of pointer to object type, and perhaps an object
as an extension? (cf. operator-> which when overloaded applies to an
object as left arg not ptr to object.) Metrowerks uniformly chokes on what
xlC accepts. I have some hunches but I'd like to hear it from someone who
actually knows.
Email response appreciated.
Thanks,
--Michael Greenspon
Sequential Inference, Inc.
www.sequin.com
mcg@sequin.com
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 1996/09/27 Raw View
mcg@wheezy.CS.Berkeley.EDU (Michael C. Greenspon) writes:
>Could someone please definitively explain the complete syntax & semantics
>of overloading operator->*(), both as a member and a global function?
>Neither ARM nor the draft std (April, '95-- where could I find a more
>recent version?) give much of a clue. The xlC compiler seems to require at
>least one of the args to be a class or enum, but shouldn't ->* be
>applicable to a left arg of pointer to object type, and perhaps an object
>as an extension? (cf. operator-> which when overloaded applies to an
>object as left arg not ptr to object.) Metrowerks uniformly chokes on what
>xlC accepts. I have some hunches but I'd like to hear it from someone who
>actually knows.
operator->*() is actually a rather ordinary binary operator with no special
restrictions. Like all user defined overloaded operators, at least one of
the two arguments must be a class object, an enum, or a reference to a
class or enum.
--
** The Klingons' favorite food was named by the first earthling to see it **
David Sachs - Fermilab, MSSG MS369 - P. O. Box 500 - Batavia, IL 60510
Voice: 1 708 840 3942 Deparment Fax: 1 708 840 3785
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]