• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Newbie coming to Cocoa from the world of C++
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbie coming to Cocoa from the world of C++


  • Subject: Re: Newbie coming to Cocoa from the world of C++
  • From: David Remahl <email@hidden>
  • Date: Wed, 5 Mar 2003 20:14:35 +0100

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];

Is this equal to doing the following C++ command:
MyObject* foo = new MyObject();

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?

Bringing 1) and 2) together, tells us that [[NSSomeClass alloc] init] is one way to instantiate an object. Another way is to use some convenience constructor, such as [NSString stringWithFormat:@"blah: %i",i] which yields an instance of an NSString (or more likely, one of its subclasses - but you shouldn't care). alloc and init* are almost always used in junction like that. There is seldom a reason to simply allocate an object without initing it.

alloc allocates memory for an object of a particular class and initializes all member variables to '\0'. Further initialization should be performed in an init method.

3) When using the @ sign in front of some quoted text, that
automatically converts that text into an NSString?

True. Those are static strings created for you when the program launches. They can never be released or modified.

4) What's the difference between an id and a Class?

id is a pointer to an object. A pointer to a class is the same as id - only more specific - but that only matters in the compilation phase.

5) Obj C has two types of methods: class and instance. Are class
methods the same as C++'s static methods?

More or less, yes. A class method (+) does not have access to any instance except to its own class (ie, self is the class object that defines the class with which the method is associated). Instance methods are related to instances of a class.

My apologies if these questions are answered in a FAQ somewhere. I
briefly looked for answers but didn't find any.

Some great resources:

<http://www.cocoadev.com>
<http://cocoa.mamasam.com>
<http://www.cocoadevcentral.com>

And while finding analogies between the two languages is always a good thing, don't overdo it. There are things that are easier to understand if you drop some of your C++ thinking temporarily (?).

/ Rgds, David
_______________________________________________
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.

  • Follow-Ups:
    • "Constant" strings are static or autoreleased?
      • From: zauhar <email@hidden>
References: 
 >Newbie coming to Cocoa from the world of C++ (From: Britt Green <email@hidden>)

  • Prev by Date: RE: Newbie coming to Cocoa from the world of C++
  • Next by Date: Re: Newbie coming to Cocoa from the world of C++
  • Previous by thread: Newbie coming to Cocoa from the world of C++
  • Next by thread: "Constant" strings are static or autoreleased?
  • Index(es):
    • Date
    • Thread