Search code examples
objective-cpointerspass-by-referenceclass-variables

Passing a class variable pointer to non-class method in Objective C


I have two classes that communicate with each other (processes in each class depend on a couple of objects from the other class). In Class1, I have a CvMat object prevMat1 declared in the interface:

@interface Class1 { 
CvMat** prevMat1;
}
@property CvMat** prevMat1;
@end

Now, I have a class method in Class2 that I am passing prevMat1 from Class1 to. I need the method within Class2 to update the value of prevMat1 and that new value be reflected in Class1. I am currently calling this method like this:

[Class2 doSomething:self.prevMat1];

Within Class2 I am handling this object under the name prevMat2. This is the function:

@implementation Class2

+ (void) doSomething: (CvMat**) prevMat2 {
//do some stuff
}

@end

The way this is currently constructed, will self.prevMat1 in Class1 be updated with the new value assigned to prevMat2 in Class2? I fear that it may not be working the way I intend it to since I am not sending a pointer, but the object itself (I think).

Thanks


Solution

  • If prevMat1 is declared as Something** then you're not sending an object when you use it as a parm, you're sending the address of pointer.

    (Hopefully you're not retaining prevMat1.)