 |
 |
Everything is a pointer!?
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: New York
Status:
Offline
|
|
Coming from a Java background in terms of Object oriented programming I'm not used to having all these objects as pointers. It seems like all objects are pointers in our programs. How do we know when to, and when to not make something a pointer in Cocoa?
------------------
Think Different.
|
|
|
| |
|
|
|
 |
|
 |
|
Admin Emeritus 
Join Date: Oct 2000
Location: Boston, MA
Status:
Offline
|
|
Statically allocated objects are not supported in Objective-C, so pretty much everything will have to be a pointer.
But since apps written in Cocoa will automatically dispose of objects as they aren't needed, pointers don't become much of a big of a deal any more.
|
|
"Against stupidity, the gods themselves contend in vain" (Schiller)
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: New York
Status:
Offline
|
|
I've seen things like NSDates though that aren't pointers
------------------
Think Different.
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status:
Offline
|
|
You are correct that every object in Objective-C is kept track of via a pointer. Objective-C and Java are the same in that they must allocate all their objects dynamically, which is something that is not true in C++. For instance:
Objective-C:
NSString *s = [[NSString alloc] initWithString:@"Hello, world"];
int i = [s length];
[s release];
Java:
String s = new String("Hello, world");
int i = s.length();
//no cleanup necessary thanks to garbage collection
C++ (dynamically allocated):
String *s = new String("Hello, world");
int i = s->length();
delete s;
C++ (statically allocated):
String s("Hello, world");
int i = s.length();
//no cleanup necessary because s will be deallocated automatically when the function returns
The designers of Java wanted to make it look like C++ so that they could get converts more easily, but since Java doesn't have any statically allocated objects, there was no reason to have the extra -> notation, so they just used the . operator for all method calls. All variables in Java are references to objects, which is really the same thing as have pointers to objects, except that they hide it from you in Java.
The long and short of it is that you don't have to worry too much, because every object in Objective-C is referred to by a pointer, so you can just treat them all the same. You would never see anything in Objective-C like *s or s.length() or s->length; they're either not accepted by the compiler or useless unless you're doing some funky pointer arithmetic, in which case you should be an expert on pointers before trying anything of the sort.
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Mar 2001
Status:
Offline
|
|
Almost all Objective C constructs are pointers, but there's no harm in ignoring that aspect of them and simply treating them as object references.
Constructs that are NOT pointers are discussed in "Types and Constants," both in the AppKit and Foundation. The most common ones are probably NSPoint, NSRange, and NSRect.
-Peter
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Nov 2000
Status:
Offline
|
|
Actually, java is the same thing. It just hides it from you,
When you do :
String s = "str";
that just makes s a pointer to a "str" wihch is somwehre in memory
its teh same as:
NSString *s = @"str" is basically the exact same thing.
-Max
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Feb 2001
Status:
Offline
|
|
NSString *s = [[NSString alloc] initWithString:@"Hello, world"];
int i = [s length];
[s release];
Objective C memory management doesn't have to be that manual; if you call autorelease on an object it will free that object in the future, presumably when you're done with it. (See NSAutoreleasePool documentation for details). So you could instead write:
NSString *s = [[[NSString alloc] initWithString:@"Hello"] autorelease];
int i = [s length];
// s will be released automatically later
Also, most ObjC classes have factory methods that return autoreleased objects, so they will go away by themselves unless you explicity retain them. So you could also use:
NSString *s = [NSString stringWithString:@"Hello"]; // returns autoreleased NSString
int i = [s length];
// s will be released later
In most cases you can use this approach and not have to write any memory management code, although there are exceptions.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Jan 2001
Location: New York
Status:
Offline
|
|
Thank you everyone for clearing this up so well! I truly do appreciate it.
Dave
------------------
Think Different.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|