Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!brl-adm!umd5!cvl!elsie!nih-csl!keith From: keith@nih-csl.UUCP (keith gorlen) Newsgroups: comp.lang.misc,comp.lang.smalltalk,comp.lang.c++ Subject: Re: C++ vs Objective-C Message-ID: <270@nih-csl.UUCP> Date: Tue, 22-Sep-87 11:06:27 EDT Article-I.D.: nih-csl.270 Posted: Tue Sep 22 11:06:27 1987 Date-Received: Thu, 24-Sep-87 06:23:26 EDT References: <3405@ece-csc.UUCP> <638@its63b.ed.ac.uk> <1811@watcgl.waterloo.edu> <1971@tekig5.TEK.COM> Organization: NIH-CSL, Bethesda, MD Lines: 111 Summary: Why C++'s support for O-OP blends better with C, and why I believe it is a superior programming language Xref: mnetor comp.lang.misc:675 comp.lang.smalltalk:326 comp.lang.c++:443 > >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" ]; > > In C++ you can define a class String to implement dynamic character strings. The data structure chosen can be much more complex than a char*. If properly done, you can create an initialized String as follows: String myString = "Object-Oriented Programming"; But here's the part you can't do in Objective-C -- you can define implicit type conversions for class String; in particular, you can define the constructor: String::String(const char*); Which the compiler will call automatically to convert char*s to Strings, and you can define the member function: String::operator const char*(); which the compiler will call automatically to convert Strings to char*s. Thus, if you have a function foo(String), you can write foo("Hello, world") -- C++ recognizes that foo() needs a String as its argument and calls your constructor to make one from the char* "Hello, world". Similarly, you can use your Strings in place of char*s: if (strcmp(myString,"hello") == 0) [Actually, a C++ programmer would overload ==, !=, >, and so on to do String comparisons; e.g., you would write: if (myString == "hello")] This feature lets a programmer blend the data structures used for his own objects with those needed by the environment. Another important point is what you HAVE to do in Objective-C that you often don't in C++: free the string when your'e done with it: Objective-C: foo() { id myString; myString = [ String str:"Object-Oriented Programming" ]; // code that uses myString [myString free]; // free the String } C++: foo() { String myString = "Object-Oriented Programming"; // code that uses myString } C++ understands that myString is an auto, and frees it automatically when it goes out of scope. > 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. Talking about what is do-able is pointless -- any language with the power of a Turing machine can do anything, theoretically. The question is what things does a language make it easy and pleasant to do. C++ features such as strong type checking and references make it a better C for those not doing O-OP. Features such as classes, function and operator overloading, user-controlled implicit type conversion, and guaranteed initialization/finalization make it far better for programming with abstract data types than Objective-C or Modula-2, for example. And derived classes and virtual functions make C++ as good or better than Objective-C for O-OP. > I will mention though, that Objective-C has a source-level debugger > for it, called Vici. What C++ currently lacks are the symbolic debugger and the extensive, mature, supported class library that Objective-C has. But these are just a matter of time. -- Keith Gorlen phone: (301) 496-5363 Building 12A, Room 2017 uucp: uunet!mimsy!elsie!nih-csl!keith National Institutes of Health Bethesda, MD 20892