Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!usc!cs.utexas.edu!uunet!rosie!news From: news@NeXT.COM (news) Newsgroups: comp.sys.next Subject: Re: C++ vsus Objective-C Keywords: objc Objective-C C++ Message-ID: <253@rosie.NeXT.COM> Date: 7 Feb 91 00:06:19 GMT References: <1991Feb6.094307.4402@nntp-server.caltech.edu> Organization: Next Computer, Inc. Lines: 91 Nntp-Posting-Host: gehenna.next.com 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