Topic: Reg Operator Overloading


Author: "Paul Mensonides" <pmenso57@attbi.com>
Date: Tue, 9 Apr 2002 16:02:26 GMT
Raw View
"Stephen Clamage" <stephen.clamage@sun.com> wrote in message
news:7ek4bug2lfnmftvj7erbcs5uaeeotbqnt7@4ax.com...
> This argument is not particularly strong, but it is the reason.
>
> Creating a temp for other operators that can be free functions is much
> less likely to be a problem, and it simplifies writing overloaded
> numerical operators.

Now that I think about it, there is another good use for the assignment operator
outside of a class.  It would make user-defined conversion operators more
useful--specifically, more safe:

template<class T> class smart_ptr {
    private:
        T* m_ptr;
        // ...
    public:
        inline operator T*() const {
            return m_ptr;
        }
        // ...
};

template<class T> void operator=(T*, smart_ptr<T>&); // no def.

If you could do this, someone would have to go to greater lengths to actually
store the pointer itself.  Obviously, not to hard:

template<class T> inline T* get_implicit_ptr(T* ptr) {
    return ptr;
}

However, at least it wouldn't happen implicitly.  I guess I'm saying it should
be allowed to that it can be disallowed. :)

Paul Mensonides

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Tue, 9 Apr 2002 16:03:38 GMT
Raw View
In article <2e4f55a0.0204080135.1476bb25@posting.google.com>, Anand
Arumugam <mailanand@lycos.com> writes
>Hi All,
>
>I have a doubt in operator overloading:
>
>Why the overloaded operators =, (), [] cannot be declared as friends
>and should be a member to the class in which they are defined? Please
>clarify.

  The friend concept is purely to allow other access to non-public
members and is unrelated to look-up rules so would add nothing. So your
question is really why those operators cannot be overloaded by free
functions.

The language designers did not want to support conversions and
promotions on the left-hand operand of operator =, nor such on the
operand of () and [].
--
Francis Glassborow      ACCU
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: mailanand@lycos.com (Anand Arumugam)
Date: Tue, 9 Apr 2002 00:17:19 GMT
Raw View
Hi All,

I have a doubt in operator overloading:

Why the overloaded operators =, (), [] cannot be declared as friends
and should be a member to the class in which they are defined? Please
clarify.

Thanking you in advance.

Regards
Anand

---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Stephen Clamage <stephen.clamage@sun.com>
Date: Tue, 9 Apr 2002 04:29:23 GMT
Raw View
On Tue,  9 Apr 2002 00:17:19 GMT, mailanand@lycos.com (Anand Arumugam)
wrote:

>I have a doubt in operator overloading:
>
>Why the overloaded operators =, (), [] cannot be declared as friends
>and should be a member to the class in which they are defined? Please
>clarify.

This question gets asked so many times it ought to be in the FAQ.

Copy-assignment (with operator=) is predefined for all structs and
classes. If you write your own, replacing the default version, the
declaration should be visible everywhere the class definition is
visible, to ensure you don't get conflicting versions of assignment in
the program. That's one reason for requiring it to be a class member.

The other reason applies to to the other operators you mention.

The left operand of a member operator must be of the class type, or a
publicly derived type. No temporary variable can be created as the
"this" argument in the function.

But the left operand of a non-member function can be of any type that
can be converted to the formal operand type, which might mean creating
a temporary object, and applying the operator to the temp.

class U;

class T {
public:
 T(const U&);
};

int operator[](const T&, int); // supposing this were legal

class U {
// no operator[]
};

int main()
{
 U u;
 ... u[10] ...; // ???
}

The last line would create an anonymous temporary of type T
constructed from u, then apply T's operator[] to it. It seems unlikely
that is what you would want. Hence the requirement that operator[] be
a member function, generating an error for the expression u[10].

This argument is not particularly strong, but it is the reason.

Creating a temp for other operators that can be free functions is much
less likely to be a problem, and it simplifies writing overloaded
numerical operators.
---
Steve Clamage, stephen.clamage@sun.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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: "nmtop40" <neil@nmtop40.homechoice.co.uk>
Date: Tue, 9 Apr 2002 15:07:47 GMT
Raw View
>
> int operator[](const T&, int); // supposing this were legal

I don't think that was the intended. What would be intended might be

T operator[] ( const Y& y, const X& x ); // illegal

where X is my class and Y is a third-party class, and T is my
chosen return type for this action.

Now I am  "extending" class X in the same way you can "extend"
Y with the external + operator.

T operator+( const Y& y, const X& x ); // legal

where again X is my class, Y is someone else's class, T is my chosen
return type for this operation.




---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]