Article View: comp.lang.misc
Article #4860Re: C++ vs Objective-C
From: Tom Milligan
Date: Mon, 21 Sep 1987 16:34
Date: Mon, 21 Sep 1987 16:34
119 lines
6115 bytes
6115 bytes
kdmoen@watcgl.waterloo.edu (Doug Moen) writes:
>csrdi@its63b.ed.ac.uk (Rick Innis, CS4) writes:
>>... I'd also like C++, and may well end up porting
>>that myself, but I've been told that Objective-C is a better language -
>>any versions available?
>
>Strange... My understanding is that C++ is the better language.
>
>I've used C++, but not Objective-C. I have talked to someone who
>spent a significant amount of time using Objective-C, and who warned
>me against it. Here are my impressions:
>
>Objective-C is apparently C with Smalltalk code embedded using escape
>sequences. The problem is that when you program in Objective-C, you
>have to deal with two universes: the C universe, and the Smalltalk universe.
>The two universes obey very different laws; code and data exist
>in either one universe or the other. Of course, there are various
>interfaces between the C and Smalltalk parts (eg, Smalltalk object pointers
>can be stored in C variables), but it's a lot messier than programming
>in a single unified language.
Both Objective-C and C++ are C language Preprocessors. Thus the capablility
exists in both to write "standard" C code. And both produce as their output,
"standard" C code. Both provide extenstions to the C language to support the
Object-Oriented constructs required for a language to be called
Object-Oriented. The C++ extensions appear to be syntactically something of
an offshoot of Simula. The Objective-C extensions follow Smalltalk
syntactic notations. This is syntax only though. Beneath all of it,
though, BOTH ARE C not "C and Simula", or "C and Smalltalk". The notion that
in Objective-C you have to deal with "Smalltalk objects" and/or "C objects"
and that this makes things un-unified is bogus. There are only C structures
in both C++ and Objective-C.
>For example, if you want to
>manipulate character strings, you can use either Smalltalk strings or
>C strings; the two kinds of strings obey different rules, and you might
>occasionally have to worry about converting between the two string formats.
>
This also is bogus. I quote from the Objective-C reference manual:
Strings are used to hold a null-terminated array of ASCII characters
(ie. by conventional C usage, " a string".
There is a structure more complex than just a "char *" that defines strings,
but this is true in both C++ and Objective-C. See pages 184-185 in the
Stroustrup C++ book for an example of this.
To create a string object initialized to "Object-Oriented Programming" in C++
or Objective-C the syntax is as follows:
C++
myString = string("Object-Oriented Programming")
Objective-C
myString = [ String str:"Object-Oriented Programming" ];
One nice thing that Objective-C does not have that C++ does is operator
overloading. Thus in C++, "[]" can be defined as a operator on string
objects that is defined to behave in a reasonable way for strings (ie. used
to index into the string). Thus, assuming that "[]" has been defined as an
operator on C++ Strings, then to run through a string looking for the letter
"X" the following code fragment would do it.
C++
boolean found;
for (i=0; i < myString.length; i++) {
if (myString[i] == "X") {
found = TRUE;
break;
}
}
Objective-C
boolean found;
for (i=0; i < [ myString size ]; i++) {
if ([ myString charAt:i ] == "X" {
found = TRUE;
break;
}
}
There are internal implementation factors that I believe make C++ the more
efficient language in this particular example, but what the heck, CPU
cycles are cheap :-)
>C++, on the other hand, is a single unified language.
>Classes are a straightforward extension of structures. This means
>you can take an existing C structure definition, and simply add
>methods to it, without invalidating existing code that uses the structure.
>In Objective C, you would presumably have to translate the structure
>definition into Smalltalk, and change all the code that used the structure
>into Smalltalk code.
>
Once again, bogus. One of the primary principles of Object-Oriented
languages is this expanability. If you have an existing class that you know
to work like you want it to, you create a subclass of that class and add the
methods or variables that differentiate it from the parent. If you don't
want to create a new subclass, but indeed want to modify the parent class
directly, it is the same in both languages: add the methods and/or instance
variables that you want to add, and then recompile and link. If you want to
hack directly with the structure that the language maintains to represent
an object, then things get more complex and you lose portability and
encapsulability (sic) in both languages. There is no translation between
Smalltalk objects and Objective-C objects, and no requirement to change
"all the code that used that structure".
In summary, both languages have what it takes to be an object-oriented
language. Objective-C uses a Smalltalk-based syntax and C++ uses something
akin to Simula syntax. As with all programming languages, what is do-able in
Objective-C is do-able in C++, or even C for that matter. As for which
language is better, well, I let others more knowlegable than myself hash that
one out. I will mention though, that Objective-C has a source-level debugger
for it, called Vici.
Tom Milligan
Technical Details
Message-ID:
<1971@tekig5.TEK.COM>Path:
arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!mit-eddie!uw-beaver!tektronix!tekig!tekig5!tommReferences:
<3405@ece-csc.UUCP> <638@its63b.ed.ac.uk> <1811@watcgl.waterloo.edu>Show Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!mnetor!uunet!husc6!mit-eddie!uw-beaver!tektronix!tekig!tekig5!tomm From: tomm@tekig5.TEK.COM (Tom Milligan) Newsgroups: comp.lang.misc,comp.lang.smalltalk,comp.lang.c++ Subject: Re: C++ vs Objective-C Message-ID: <1971@tekig5.TEK.COM> Date: Mon, 21-Sep-87 12:34:26 EDT Article-I.D.: tekig5.1971 Posted: Mon Sep 21 12:34:26 1987 Date-Received: Wed, 23-Sep-87 04:37:04 EDT References: <3405@ece-csc.UUCP> <638@its63b.ed.ac.uk> <1811@watcgl.waterloo.edu> Reply-To: tomm@tekig5.UUCP (Tom Milligan) Organization: Tektronix, Inc., Beaverton, OR. Lines: 119