EDIT: Oops! This thread should read: "Why doesn't this object need memory allocated?" 
Hi guys!
Not sure where my other thread went but I can't find it so I'm reposting the same question:
I'm coming from C++ and learning Objective-C and was working through some exercises and this one has me stumped on why its working.
How come the last object does not need initialized the same as the others? I can initialize it and it works but if I do not initialize one of the first two objects (c1 and c2) the program wont work. I don't understand how this works seeing as they are all objects of the same type.
Could anyone explain this to me please?
Hi guys!
Not sure where my other thread went but I can't find it so I'm reposting the same question:
I'm coming from C++ and learning Objective-C and was working through some exercises and this one has me stumped on why its working.
Code:
#import <Foundation/Foundation.h>
#import "Complex.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Complex *c1 = [[Complex alloc] init];
Complex *c2 = [[Complex alloc] init];
Complex *compResult; //<---Why doesnt ths need initialized like the above two objects?
[c1 setReal: 18.0 andImaginary: 2.5];
[c2 setReal: -5.0 andImaginary: 3.2];
//Add and print two complex numbers
[c1 print]; NSLog(@" +"); [c2 print];
NSLog(@"------");
compResult = [c1 add: c2];
[compResult print];
NSLog(@"\n");
[c1 release];
[c2 release];
[compResult release];
[pool drain];
return 0;
}
How come the last object does not need initialized the same as the others? I can initialize it and it works but if I do not initialize one of the first two objects (c1 and c2) the program wont work. I don't understand how this works seeing as they are all objects of the same type.
Could anyone explain this to me please?