Objective C++: A History

Last week, we gave an overview of how a little sprinkling of C++ could make your Cocoa programming a lot sweeter.  At the end we promised to delve more into details in future blog posts.  So, here we go!  We're going to begin with a little bit of relevant history.



Sherman, set the Wayback Machine for 1958.  That was the year John McCarthy at MIT invented the Lisp programming language.  Lisp (which I believe is an acronym for "Lots of Inane Silly Parentheses") was originally an academic exercise in building an algorithmic language, but people began using it for actual programming, especially in AI research labs, especially after the first complete Lisp compiler came out in 1962.

Almost-meanwhile, a Cambridge bloke named Martin Richards was developing a language called BCPL.  This was intended to be used for writing compilers for other languages, and introduced that great programming innovation, the curly brace.  It inspired the B language, developed by Ken Thompson and Dennis Ritchie at Bell Labs, around 1969.  This, in turn, evolved quickly into C, a little language you may have heard of.  That happened, still at Bell Labs, mostly in 1972.  So powerful was C that most of the Unix cernel was rewritten in C starting in 1973.

Also in the early 1970s, Alan Kay at Xerox PARC started developing a language called Smalltalk.  This was heavily influenced by Lisp (for the language geeks in the audience: it borrowed code blocks and closures).  Smalltalk evolved rapidly and by 1980, embraced object-oriented programming as a core feature.  Everything was an object, and objects could hold state, receive messages from other objects, or send messages from other objects.  This version of Smalltalk (Smalltalk-80) was given to a small number of firms (including both Apple and ITT Corporation) for "peer review."

So let's pause for breath a moment and review.  It's the early 80s; C is powerful, but Smalltalk is trendy and cool.  Apple's line of Apple II computers is doing well, but behind closed doors, they're working on the Macintosh.  Apple knows about Smalltalk, but (my guess here) it's probably considered not mainstream enough; Mac (and Apple IIGS) programming at the time is based instead on Pascal, a widely popular language at the time, though not one that's object oriented.  The Mac comes out, of course, in 1984.

The next year, though, was a rough one, and in 1985, Steve Jobs was forced to resign from Apple.  He wasted no time in founding NeXT, which would make a shiny new computer that was like a Mac on steroids, and set his sights on beating Apple at their own game.  But a computer isn't just hardware — the OS and APIs are critical too.  What would he use?

While all that was going on, Brad Cox and Tom Love — both formerly of ITT Corporation (who, you may recall, got a copy of Smalltalk 80) — developed a language on top of C but with features borrowed from Smalltalk.  These features included both functionality and syntax, resulting in the weird chimera of Objective C that we all know and (ahem) love.  They published it first in 1986.

At virtually the same time, Bjarne Stroustrup at Bell Labs came out with another object-oriented variant of C.  This was C++, of course.  It was started around 1979, and the first implementation was released in 1985.  So this was slightly ahead of Objective-C, but not by much.

So, here was Steve Jobs needing a trendy and cool language to go with his trendy and cool new Mac competitor.  It had to be object oriented, of course, and to be taken seriously, it needed to be some derivative of C.

Of course I'm not privvy to whatever considerations went through Steve's head (though I suspect things like "trendy" and "cool" were involved).  But for whatever reason, in 1988, NeXT licensed Objective C, and the die was cast.  All NeXT app development was done in Obj-C.

Despite the NeXT's coolness (and trendiness), it never really caught on, selling about 50,000 units total in its ten-year lifetime.  One can imagine this sticking rather painfully in the craw of Jobs.  But this failure was short-lived, as in early 1997, Apple bought NeXT and brought Jobs back into the fold.  Jobs brought several high-level execs from NeXT with him, and proceeded over the next several years to turn NeXTSTEP (the NeXT operating system and software environment) into Mac OS X.  In a somewhat roundabout way, Jobs succeeded in replacing the classic Mac environment that had ousted him with his own Mac competitor, NeXT, and earned the right to feel vindicated on all the design decisions that went into it.  Those decisions include, of course, Objective C.

As a result, we're still using Objective C today to write Mac OS X and iPhone apps.  Without this twisted tale, it's clear that Objective C would have faded into obscurity almost as soon as it was written; it's never been used seriously for anything outside of the NeXT/Apple environment.  The rest of the C-derived programming world settled on C++ (and more recently, newer languages like Java and C#).

That's not to say that Objective-C doesn't have some interesting features.  It's a more dynamic language, allowing tricks like invoking methods that were unknown at compile time, or looking up properties at runtime by name.  But it does suffer from certain shortcomings of its old C heritage, especially when it comes to higher-level data types like strings, 2D or 3D points, and so on.  C++ added operator overloading and related features that allow us to add these new types, and make them just as easy to read and write as base types.  Objective-C, lacking those features, implements those types as either structs or Obj-C classes, and in either case requires cumbersome function or method syntax for anything you might want to do with them.

If our story ended there, it would be a sad tale of sibling languages separated at birth, and never meeting again.  But then, in 2002, a miracle happened.

In that year, engineers at Apple quietly released a variant of the compiler they called "Objective-C++".  As they put it, this "allows C++ constructs to be called from Objective-C classes, and vice versa."  The two estranged siblings were finally reunited.

As explained last week, this combination really is revolutionary.  Objective-C++ is Objective-C, and it is also C++.  You can use all the standard Cocoa stuff you're used to.  But you can also make it better, with judicious use of operator overloading, strong typechecking, integration with open-source libraries written for C/C++, etc.  You can even (if you're really feeling nutty) write portable code that runs on both Mac/iOS and on other platforms, with relatively little platform-specific code.

ObjC++ has been slow to catch on.  Apple doesn't make much noise about it, for reasons that should be clear when you consider the history.  But it works well, and more and more Cocoa developers are starting to realize the benefits.

Next week we'll dispense with the academics, and get down into the nitty-gritty of really using this stuff.  Stay tuned!

 





Please add a comment

Posted by Steffen on
Hi,

It's really great that somebody sometimes takes time into account. There is an - often little known - reason for many thing, including programming language concepts and conventions. Here you do it for Obj-C, and thereby relieves the stress. I thought I was stupid. That may still be true, but Obj-C really is old-fashioned in some ways.

Also, I need to do audio. There I'll need to use C, C++ and Obj-C. Your tutorial has really helped me see, that the road to audio is bumpy and I in fact do get the point.

Thanks.

Steffen
Posted by Kévin Aujoulet on
Nice article, Bravo !
Leave a Reply



(Your email will not be publicly displayed.)

Please type the letters and numbers shown in the image.Captcha Code