[comp.sys.next] C++ vsus Objective-C

fjs@nntp-server.caltech.edu (Fernando J. Selman) (02/06/91)

Considering that C++ is the most widely available of the
two, Why did next choose Objective-C? I do not know OO programing,
so if someone could summarize the differences I would
appreciate it. Thank you,

				- Fernando J. Selman

news@NeXT.COM (news) (02/07/91)

In article <1991Feb6.094307.4402@nntp-server.caltech.edu>  
fjs@nntp-server.caltech.edu (Fernando J. Selman) writes:
> 
> Considering that C++ is the most widely available of the
> two, Why did next choose Objective-C? I do not know OO programing,
> so if someone could summarize the differences I would
> appreciate it. Thank you,
> 
> 				- Fernando J. Selman

Here are two NextAnswers that may be helpful:

objc Objective-C C++

Q:  Why did NeXT choose Objective-C rather than C++?

A:  NeXT chose Objective-C over C++ for several reasons:
 
(1)  We wanted a language which represented the smallest perturbation
to the C language, to make it easier for programmers to learn.  In
our opinion, C++ was a significantly major change to the C language
compared to Objective-C, which adds only a few new constructs to C.

(2)  At the time we made the decision, C++ did not support run-time
binding, and this lack greatly reduces the advantages of taking an
object-oriented approach.  With run-time binding, you need not
know the details of the object to which you're sending a message.
This supports modularity and reusability of code, and is essential
for a true object-oriented programming environment.  See Chapter
Two of the Brad Cox book (Object-Oriented Programming: An Evolutionary
Approach; Addison-Wesley, 1987) for a fuller description of these
issues.

(3)  C++ does not support dynamic loading of objects, once again
a key feature necessary in order to take full advantage of the
power of Interface Builder.

QA342

objc

Q:  How does one mix C and Objective-C code?  How can one call Objective-C from  
C?

A:  Just do it!  C functions can freely send Objective-C messages, and  
Objective-C methods can freely call C functions.   For an example, look at  
main() of one of the applications in /NextDeveloper/Examples.  

If you place Objective-C code in a ".c" file,  you need to compile the file  
with the Objective -C option.  The simplest way to do this is to change the  
".c" suffix to ".m", and then add the file name under ".m (other)"  in IB's  
Project Inspector.    If it's not possible to change the suffix, compile the  
file using the -objc compiler flag. 

You can place any C code in a ".m" file without any special handling.  The C  
code will need to extern id's as per regular C scoping rules.

Only within Objective-C methods can you directly access instance variables and  
the hidden variable "self."  You must pass an object to a C function that needs  
access to it.   For example, this C function will return the width of a View:

NXCoord widthOfView(View *self)
{
    NXRect b;
    [self getBounds:&b];
    return b.size.width;
}

You may access public instance variables like this:

void setNumber(MyObject *self,int newValue)
{
    self->number = newValue;
}

Remember that all instance variables are considered private unless explicitly  
declared public.  However, within the confines of a class implementation, all  
instance variables of that class are considered public.  So in the  
implementation for MyControl (a subclass of Control), you could write a C  
function to set the tag of a MyControl object like this:

void setTag(MyControl *self,int newTag)
{
    self->tag = newTag;
}


QA27

Chris MacAskill
cmac@next.com

news@NeXT.COM (news) (02/07/91)

Whoops.  In my last posting I included a NextAnswer that showed how to  
integrate C and objective-C.  The question was about mixing C++ and  
Objective-C.

Here's one for that:

C++ integration

Q:  Can I integrate C++ code into my Interface Builder/Objective-C application?   
How?

A:  Yes, in release 2.0 you can, and it's pretty easy (once you know how)!  The  
procedure breaks down into three categories of things that you must do:  
compiling, Interface Builder and getting the two languages to talk to each  
other.

[... goes into detail here ...]

There is an example located in /NextDeveloper/Examples/Calculator++ which  
illustrates the integration of Interface Builder nib files, Objective-C source  
code, and C++ source code into one program.

QA584

Chris MacAskill
cmac@next.com

shawn@chaos.cs.brandeis.edu (Shawn Broderick) (02/10/91)

Also, at the time that NeXT made the Obj-C / C++ decision, C++ was
nowhere near as widely used as it is now.

Shawn Broderick
BrennerBroderick, Inc.
shawn@chaos.cs.brandeis.edu
merk!bb!shawn