Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reg Operator Overloading

26 views
Skip to first unread message

Anand Arumugam

unread,
Apr 8, 2002, 8:17:19 PM4/8/02
to
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...@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Stephen Clamage

unread,
Apr 9, 2002, 12:29:23 AM4/9/02
to
On Tue, 9 Apr 2002 00:17:19 GMT, mail...@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...@sun.com

nmtop40

unread,
Apr 9, 2002, 11:07:47 AM4/9/02
to
>
> 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.

Paul Mensonides

unread,
Apr 9, 2002, 12:02:26 PM4/9/02
to
"Stephen Clamage" <stephen...@sun.com> wrote in message
news:7ek4bug2lfnmftvj7...@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

Francis Glassborow

unread,
Apr 9, 2002, 12:03:38 PM4/9/02
to
In article <2e4f55a0.02040...@posting.google.com>, Anand
Arumugam <mail...@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

0 new messages