From: jac@moonshine.llnl.gov (James A. Crotinger)
Newsgroups: comp.sys.amiga.programmer
Subject: Re: Obj-C vs. C++ [was Re: Comeau C++]
Date: 29 Sep 92 06:18:26 GMT
Organization: Magnetic Fusion Energy - LLNL

dnavas@oracle.uucp (David Navas) writes:
> C++, it seems to me, was designed to provide an object-oriented >interface<
> to C, where Obj-C was designed as an object-oriented language with similar
> syntax as C.

  I guess this is a matter of opinion. Obj-C always looked like someone
tried to graft small-talk on top of C. C++ seems more C-like, and if you
like C syntax (I do), it seems more elegant.

> I'm not sure how stringent Obj-C type-checking is, but it's worth
> noting that if your methods are bound at run-time, it'd be rather
> difficult to type-check them at compile time....

  This is not true. C++ does have run-time binding, and it is type-safe.
Suppose class D inherits from B, which declares a "virtual function" 
named "foo". Class D can provide its own definition of "foo". Now,
consider the following function:

     void test(B &b)
     {
        b.foo();
     }

Which foo gets called? B::foo() or D::foo()? This decision is made
at run time, depending on the type of the object which is actually passed
to test:

      D d;
      B b;
      B *pb = &d;

      test(b)    // calls b.B::foo()
      test(d)    // calls d.D::foo()
      pb->foo()  // also calls d.D::foo(), even though pb is a
                 // pointer to the base class.

This is all type safe! The compiler has seen the declarations of
B and D. It knows that the B class understands the message "foo",
and that any class which publicly inherits from B will also understand
this message.

What C++ won't let you do is to send a message to an object which
the compiler doesn't know anything about. For example, suppose D
defined a message "bar" which was not defined in B. Then the following
code is illegal:

      D d;
      B *pb = &d;
      pb->bar();

My understanding is that Objective C will allow something like this.
However, what would happen if pb pointed to a B? This is not a safe
program. You might say that you'd only do this if you knew that pb
pointed to a D. Well, if you do know this then you can cast the pb to
a "pointer to D" and make this work. However, if you know this then
your pointer should be a pointer to D in the first place. Frankly,
I've never seen a concrete example of something that Obj-C can do in
an "object oriented" manner, that C++ can't. The only possiblity that
I can think of is that it might be somehow possible to "dynamically
link" new messages into a running program, and then send these
messages to existing objects. Is this possible? Do you have any other
examples?

> At the same time, I don't understand how you can possibly claim that
> an object-oriented language is polymorphic when methods are bound at
> compile-time.

  See the above. C++ is polymorphic.

> Depends.  Again, remember that there are competing design goals.
> C++ produces code that is -essentially- not object-oriented, but it
> is created in an environment that is [God, I feel the flames
> already, why do I make such wild generalizations?]. n

  I do agree that polymorphism is an absolute necessary feature of an
"object oriented" language; however, as explained above, C++ has this
feature.  Perhaps after reading my above explaination, you'd like to
reconsider this statement. You can write completely object oriented
code in C++!

  Jim
--
-------------------------------------------------/\--------------------------
James A. Crotinger     Lawrence Livermore N'Lab // \ The above views are mine
jac@moonshine.llnl.gov P.O. Box 808;  L-630 \\ //---\  and are not neces-
(510) 422-0259         Livermore CA  94550   \\/Amiga\  sarily those of LLNL.
Newsgroups: comp.sys.amiga.programmer