Topic: The worth of operator overloading (was Prefix versus postfix operator overrides)


Author: jamshid@ses.com (Jamshid Afshar)
Date: Wed, 17 Aug 1994 06:10:22 GMT
Raw View
Redirected to c.l.c++.

In article <rfgCu5qur.8Er@netcom.com>,
Ronald F. Guilmette <rfg@netcom.com> wrote:
>In article <CtwLv2.E54@cwi.nl> olaf@cwi.nl (Olaf Weber) writes:
>>[...]  Is operator overloading itself worth the trouble?
>>Try writing a template that works effiently with both the built-in
>>types and user-defined types given that neither references nor
>>operator overloading are available.  You'd have to introduce lots of
>>littel "wrappers" for the operators of the built-in types, and hope
>>the compiler does inline them.
>
>So?  So what?  Do you have a problem with that?  Would it really have
>bothered you if the language had been defined such that all standard
>conforming implementations had been required to pre-define things like
>`plus(int,int)' and `plus(double,double)' in addition to the correspon-
>ding built-in operators??  If so, why?

Yes, it would have bothered me a great deal because I was a C
programmer, not a LISP or Modula 2 programmer.  If you expect me to
use a language that is an extension to C instead of learning a whole
new language, I want something that's "natural" to me and takes
advantage of things I'm already used to doing (and maybe even enjoy).
I don't want to write a Matrix class template whose implementation
bears no resemblance to my old IntMatrix class or to my even older C
function:

 void add_matrix(int **m1, int **m2, int** result);

Nor do I want to change the syntax I've always used to access the
elements of a string in order to take advantage of a string class.

The more I think about it, the more "natural" operator overloading
seems to be for C++.  After all, C had it long before C++.  Remember,
not all languages allow you to operate on integers and reals using the
same operators.  C "overloaded" the operator `+' for all combinations
of the many different numerical types, along with overloading it for
some pointer types and some numerical types.

Jamshid Afshar
jamshid@ses.com