objective c - Object variable and Object syntax -


if have following in main file

int main ( int argc, const char * argv[])     {         @autoreleasepool         {             complex * c1 = [[complex alloc] init];             complex * c2 = [[complex alloc] init];              complex * compresult;              compresult = [c1 add: c2];              [compresult print];         {           return0;              {  /**implementation of add method **/ -(complex *) add: (complex *) f {     complex *result = [[complex alloc] init]     result.real = real + f.real;     result.imaginary = imaginary + f.imaginary;     return result; }  

i know c1 , c2 objects, compresult considered variable until compresult = [c1 add: c2];

my assumption here add method returns object , doing compresult = [c1 add: c2]; setting compresult equal object. turn compresult object?

so in mind compresult variable receives result of [c1 add: c2], when [compresult print], confused because thought can use syntax when sending message (in case print) object?

i guess main question after compresult = [c1 add: c2]; variable compresult holding/representing object or become object????

c1 , c2 not objects, they're pointers objects. similarly, compresult pointer, @ time of declaration it's uninitialized, meaning doesn't point specific object.

when invoke add: method via [c1 add:c2], new object created , pointer returned. pointer assigned compresult. compresult still pointer, c1 , c2, points newly created object, meaning can send messages object (like [compresult print]) did [c1 add:c2] before.

editorial note: more idiomatic return autoreleased object add: rather retained 1 you're using right now.


Comments