Topic: operator&() silly overload?


Author: mwoolf@cix.compulink.co.uk ("Matthew Woolf")
Date: Fri, 23 Dec 1994 23:50:18 GMT
Raw View
What is the opinion on overloading unary& - it seems silly to me as it
allows the class developer to return an address other than the object
current one. If it's not silly when is it useful?

Surely it's like allowing the developer to overload '*' for a pointer!




Author: "Ronald F. Guilmette" <rfg@rahul.net>
Date: 24 Dec 1994 09:59:38 GMT
Raw View
In article <D1AFJv.nw@cix.compulink.co.uk>,
Matthew Woolf <mwoolf@cix.compulink.co.uk> wrote:
>What is the opinion on overloading unary& - it seems silly to me as it
>allows the class developer to return an address other than the object
>current one. If it's not silly when is it useful?

Allowing programmers to overload unary & is one of those cases where
Bjarne broke his own rules and made the language not merely extensible,
but `mutable'.  (By overloading this particular operator for a given
class type you can actually _change_ the meaning of an existing, built-in
operator.)

>Surely it's like allowing the developer to overload '*' for a pointer!

No.  Overloading unary & is worse.  In the case of overloading unary *,
you are not actually changing the meaning of an operator WHEN APPLIED
TO OPERAND(s) OF A GIVEN TYPE FOR WHICH THE SAME OPERATOR ALREADY HAD
A WELL-DEFINED, BUILT-IN MEANING.

--

-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- E-mail: rfg@segfault.us.com ----------- Purveyors of Compiler Test ----
-------------------------------------------- Suites and Bullet-Proof Shoes -




Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Sat, 24 Dec 1994 15:50:18 GMT
Raw View
In article <D1AFJv.nw@cix.compulink.co.uk> mwoolf@cix.compulink.co.uk ("Matthew Woolf") writes:
>What is the opinion on overloading unary& - it seems silly to me as it
>allows the class developer to return an address other than the object
>current one. If it's not silly when is it useful?
>
>Surely it's like allowing the developer to overload '*' for a pointer!

 There is a simple use of operator&, and that is to
allow you to take the address of a temporary object, which is
useful. Do not blame me that this technique is error prone
and invasive -- at least it works.

 struct X { X* operator & () { return this; } };
 f(X*);
 f(&X()); // fine now!

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189




Author: warrens@ix.netcom.com (Warren Seltzer)
Date: 25 Dec 1994 06:50:48 GMT
Raw View
 (John Max Skaller) writes:

> struct X { X* operator & () { return this; } };
> f(X*);
> f(&X()); // fine now!


 How do you distinguish overloading prefix operator& from
postfix operator &  ??

 Like this?

     struct X { X& operator & (int dummy) { return *this; } };


 Just asking ;-)


 Warren S.