LANGUAGE:
I am writing an object-oriented code in MATLAB. I wrote almost all of it, and now when trying to test it, am running into what looks like a very fundamental issue.
BACKGROUND OF THE CODE:
I have a class Window and a class Tracker. Both are subclasses of the Singleton class (that is, they have private constructors to ensure that only one single instance of class Window and class Tracker are created).
I instantiate each of them- so I now have a myWindow and myTracker object.
In my main script, I call a method, myWindow.isNewbead(). isNewbead is a public method of class Window.
That’s the scene. Now the problem:
THE PROBLEM:
Inside isNewbead(), I call myTracker.getpredictedPositions(). getpredictedPositions() is a public method of class Tracker. However, when I run this line, I get an error saying variable ‘myTracker’ is undefined. And sure enough, I look in the variable workspace, and the only variables there are the local variables INSIDE myWindow.isNewbead();
So I now have two questions:
QUESTIONS:
-
Is this true of OOP everywhere? That is to say, can you not call a public method on an object from inside a method in another object without explicitly passing the first object to the method of the second object? It seems like a lot of hassle to me, because I use properties and methods of lots of objects of different classes in every method, so I will have to end up passing hundreds of objects every time!
-
If this is only a MATLAB-specific problem (like the problem of no-static-variables) then how do I work my way around it?
Thank you so much!
Regards.
Yes, you can call a public method of one object (object A) from a method of another object (object B) since it’s, well, public; however, you need to have a reference to object A to use within the object B method. Pass a reference to the object A as input argument to the object B method.
It may seem like a hassle but how else could this work? Even though Tracker inherits from the Singleton class, myTracker is still just an instance of the Tracker object. You need a reference to this instance in order to use its methods.