🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.objective-c
Article #222

Re: Operator overloading

#222
From: Zacharias Beckman
Date: Tue, 01 Jan 1991 16:17
74 lines
3910 bytes
In article <5952@stpstn.UUCP> lerman@stpstn.UUCP (Ken Lerman) writes:

>IMHO the polymorphism available through Objective-C qualifies as
>operator overloading.

Polymorphism is, at least in my vocabulary, different from operator
overloading.  In overloading, a single object's method (or a function) has
more than one invocation.  Operator overloading specifies that operators,
such as +, -, >, <, =, etc., can be overloaded to provide a functionality
that the programmer supplies.

This is not polymorphism.  Polymorphism is available in both C++ and
Objective-C.  My personal favorite is C++, as I do not agree with the very
loose typing of Objective-C.  I also feel that C++ is more powerful,
offering a few features which are key to me:  multiple inheritance,
overloading, operator overloading, public/private/protected keywords, and
others.  I am also looking forward with great expectations to templates and
exception handling.

>I'd be interested in hearing more details of what method overloading
>functionality is missing which lead Zacharias to write:  "I really miss
>this capability, particularly in method overloading."

As an example, here is a very simple class to show the overloading available
in C++ (keep in mind the definition above).

  class Point : public Shape {
      uint x;
      uint y;
      uint z;
    public:
      void set(int a = 0, int b = 0, int c = 0) { x = a; y = b; z = c; }
      void set(Singularity & s, int i = 0) { set(s.x(), s.y(), i); }
      void set(Point & p) { set(p.x(), p.y(), p.z()); }

      Point & operator=(Point & p) { if (p != this) set(p); }

      uint x() { return x; }
      uint y() { return y; }
      uint z() { return z; }
  };

You might want to actually have this class descend from a Singularity class
which would represent a 2-d point.  But, to keep things simple, I didn't do
that.  Obviously, the class doesn't do much.  However, it does allow the
following:

  Point myPoint;                  // declare a couple of Point objects
  Point anotherPoint;
  Singularity single;             // a singularity (a 2-d point)

  anotherPoint.set(50, 50, 50);   // set anotherPoint to [50,50,50]
  single.set(80, 80);             // presumably set singularity to [80,80]

  myPoint.set(anotherPoint);      // set myPoint to [50,50,50]
  myPoint.set();                  // set myPoint to [0,0,0]
  myPoint.set(50, 100);           // set myPoint to [50,100,0]
  myPoint.set(50, 100, 5);        // set myPoint to [50,100,5]
  myPoint.set(single);            // set myPoint to [80,80]
  myPoint.set(single, 5);         // set myPoint to [80,80,5]
  myPoint = anotherPoint;         // set myPoint back to [50,50,50]

My point (pun partially intended) is that it is a tool which makes my life
as a programmer a bit easier.  This example also shows default arguments in
action, which is also "missing" from Objective-C.  What it comes down to is
having an object Thing with the method set (in C++) or an object Thing with
the methods setInt, setFloat, setOther, etc. (in Objective-C).  C++ allows
us to be truly "object-oriented" -- we send a set method, and don't worry
about what the argument is.
-- 
Zacharias J. Beckman  |  God grant me Serenity to accept things I can't change,
Lower Code Dweller    |  Courage to change those I can,
Boss Logic Inc.       |  and Wisdom to know the difference.
zack@bosslogic.com    |  --- Sinead O'Conner.
Technical Details
Message-ID: <193@bosslog.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uunet!bosslog!zack
References: <1990Dec14.155211.6598@mprgate.mpr.ca> <191@bosslog.UUCP> <5952@stpstn.UUCP>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uunet!bosslog!zack
From: zack@bosslog.UUCP (Zacharias Beckman)
Newsgroups: comp.lang.objective-c
Subject: Re: Operator overloading
Message-ID: <193@bosslog.UUCP>
Date: 1 Jan 91 16:17:21 GMT
References: <1990Dec14.155211.6598@mprgate.mpr.ca> <191@bosslog.UUCP> <5952@stpstn.UUCP>
Distribution: na
Organization: Boss Logic Inc., Fairfield, Iowa
Lines: 74