Re: Newbie coming to Cocoa from the world of C++
Re: Newbie coming to Cocoa from the world of C++
- Subject: Re: Newbie coming to Cocoa from the world of C++
- From: "John C. Randolph" <email@hidden>
- Date: Wed, 5 Mar 2003 13:47:11 -0800
On Wednesday, March 5, 2003, at 10:30 AM, Britt Green wrote:
Hello all,
I've recently picked up Learning Cocoa with Objective C in order to
write apps for the Macintosh. I'm coming from a C++ background and have
some basic questions about Obj C that I'm hoping people can answer.
Basically I'm trying to find analogues between the two languages.
1) In Obj C, instantiating an object is done like this:
NSObject * myObject = [NSObject alloc];
That's one way, yes. Just to be canonical, the line above should also
include a call to -init, e.g:
NSObject *myObject = [[NSObject alloc] init];
Many classes also have convenience methods that return autoreleased
instances, such as [NSMutableArray arrayWithCapacity:], and the like.
Is this equal to doing the following C++ command:
MyObject* foo = new MyObject();
Similar, not quite equal.
2) In C++ one doesn't explicitly call the constructor when an object is
created. However, in Obj C one needs to call the init method, correct?
Yes.
3) When using the @ sign in front of some quoted text, that
automatically converts that text into an NSString?
To be precise, the compiler boils it down into a function call that
returns a member of a private NSString subclass. There's a compiler
option to let you choose the class to use for constant strings.
4) What's the difference between an id and a Class?
id is a pointer to an objc_object structure.
Class is a pointer to an objc_class structure.
According to the header file, objc.h:
typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
objc_class, as you might guess, is rather more elaborate:
struct objc_class {
struct objc_class *isa;
struct objc_class *super_class;
const char *name;
long version;
long info;
long instance_size;
struct objc_ivar_list *ivars;
struct objc_method_list **methodLists;
struct objc_cache *cache;
struct objc_protocol_list *protocols;
};
5) Obj C has two types of methods: class and instance. Are class
methods the same as C++'s static methods?
Essentially, yes. Class methods are executed by the class, instance
methods are executed by instances of the class.
My apologies if these questions are answered in a FAQ somewhere. I
briefly looked for answers but didn't find any.
Here's the book:
file:///Developer/Documentation/Cocoa/ObjectiveC/index.html
Or, if you don't have a machine with the apple developer tools
installed, you can find it on the web at:
http://developer.apple.com/techpubs/macosx/Cocoa/ObjectiveC/index.html
HTH,
-jcr
John C. Randolph <email@hidden> (408) 974-8819
Sr. Cocoa Software Engineer,
Apple Worldwide Developer Relations
http://developer.apple.com/cocoa/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.