Working around Global Variables and Pointers
Show older comments
I am implementing a physical problem using OOP. I have two classes called "Room" and "Particle". Room as properties like temperature, volume, etc. Particle have properties such as mass, velocity, position, etc. I start by defining a Room object and giving it properties. I then define thousands of Particle object that are supposed to move around in the room depending on the properties of the room and the particle. To simulate the movement of a given particle, I've defined a method in the "Particles" class called "MoveParticle". Everytime I call this function, the particle moves.
The MoveParticle method depends on the properties of the room that the given particle is in. How can I access this data (properties of the Room object) as the particle and room are different objects?
In C/C++, I would define a pointer in the "Particles" class that points to a specific "Room" object, so that whenever I want to access properties in of the Room object, the pointer can go and access it. But I don't think I can do this in MATLAB.
Another work around would be to define the Room object as a global variable, however, that seem to be considered a bad practice.
Other alternatives is to pass the Room object as a argument in the MoveParticle method or include Room object as a property in the Particle class. However, that seems to be computationally inefficient since a copy of Room object is created in every Particle object which consumes a lot of memory.
Thanks a lot.
Accepted Answer
More Answers (0)
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!