Hello everyone, I'm running into a slight problem. I'm working on a project currently, and I'm a bit stuck. Before I describe it, I'm aware that there are probably much more efficient ways of doing this, and I'm also aware that this is probably a relatively stupid way of going about this, but as I'm sort of new to Arduino and whatnot, this is what I'm attempting to accomplish, and I'm sure it can come in handy on other projects as well. So, on to the problem!
I have 3 controllers, an "InputController" class, a "SurfaceController" class, and lastly, a "MotionController" class. I'd like to have one more class called "Surface" that holds all the data etc for all of the moving parts (and I'm sure I'll have more than JUST a Surface class, there will probably be an Engine class, etc.), and then, as I instance them, not have to worry about having to tell InputController, SurfaceController, and MotionController that they exist. I'd like Input, Surface, and MotionController to be able to FIND all of the instances of these classes and store them for use later, as I'll have to work with them. I'm attempting to do this so that if I add a new control surface, or a second engine, or whatever I can just add a new instance of the "Surface" class etc., and I also plan on using this to make a system that detects whether or not a control surface is plugged in physically to the board, and ONLY creates an instance of the Surface class if it is.
So what am I asking? How do I, from one class, find all the instances of another class so that I can use them, without having to explicitly state "Hey these are the classes that are currently in existence"?
There is NO way to track instances of any class unless you write code yourself to do it. There is no global list of instances. Each instance of a class is created on the heap, and it is up to the programmer to keep track of them.
You can can create a static list of instances of a given class, within the class itself, and make that list publicly available so other classes can access it.
Slydog486:
So what am I asking? How do I, from one class, find all the instances of another class so that I can use them, without having to explicitly state "Hey these are the classes that are currently in existence"?
Are you trying to find these instances while you are writing the program or while the program is running?
AFAIK the hex code that is uploaded to an Arduino knows nothing about any class.
Robin2:
Are you trying to find these instances while you are writing the program or while the program is running?
AFAIK the hex code that is uploaded to an Arduino knows nothing about any class.
...R
Thanks for the reply! I'm not entirely sure what you're asking, but if you're asking WHEN in the program the classes need to be found, the classes are being instanced before the setup function. No other ones will be created once the program begins running.
Slydog486:
Thanks for the reply! I'm not entirely sure what you're asking, but if you're asking WHEN in the program the classes need to be found, the classes are being instanced before the setup function. No other ones will be created once the program begins running.
you can create a static array of pointers to the (let's say parent) class.
Slydog486:
Thanks for the reply! I'm not entirely sure what you're asking, but if you're asking WHEN in the program the classes need to be found, the classes are being instanced before the setup function. No other ones will be created once the program begins running.
Since you're creating them statically, you ALREADY KNOW at compile time how many there are and where they are. What else do you need?
Slydog486:
Thanks for the reply! I'm not entirely sure what you're asking, but if you're asking WHEN in the program the classes need to be found, the classes are being instanced before the setup function. No other ones will be created once the program begins running.
It strikes me that a text editor with a good search function is what you need. I use Geany
Robin2:
It strikes me that a text editor with a good search function is what you need. I use Geany
...R
I actually already use Visual Studio with the Visual Micro extension for this stuff. I work with Unreal Engine and Lumberyard in my spare time so I figured if there was any way at all to integrate Arduino into that I would, and thanks to Visual Micro I can!
gfvalvo:
Since you're creating them statically, you ALREADY KNOW at compile time how many there are and where they are. What else do you need?
This is a way, in my mind at least due to the way I like to write code and how I prefer to do this, to make my code that little bit more modular. It's also a fantastic way to save time. Laziness is the mother of writing less lines of code... Or something like that.
BulldogLowell:
you can create a static array of pointers to the (let's say parent) class.