I would like a function to return an array of objects. Having tried a number of approaches I have not yet managed to come to a solution that works yet.
By way of example I have written a class called person which has a static function called getTwoRandomPeople. Its purpose is to create two people and return a Person array of size 2. Please see the Person class below:
#include "Person.h"
Person::Person(String iName) {
name = iName;
}
Person::~Person() {
}
String Person::getName(){
return name;
}
Person* Person::getTwoRandomPeople(){
Person *people[2];
Person *p1 = new Person("Brian");
Person *p2 = new Person("Liam");
people[0] = p1;
people[1] = p2;
return people[0];
}
In my sketch I am attempting to access each Person and output the name. See below:
You should be aware that using dynamic object allocation like this can be problematic on Arduino since you have very little memory and very little scope for compacting the heap when it becomes fragmented. But this isn't the cause of your problem.
You could solve it by dynamically allocating the array and returning the array and its contents, and making the caller responsible for freeing them all. It's taking you further down the dangerous path of using dynamic memory allocation, and I'd advise against it, but that's the simplest approach to return an array of unknown size. If the return array is a predictable size, simply have the caller provide it as an argument passed in to the method.
It looks like a great solution. I will try that at the weekend.
John,
I had not mentioned in my original post that I had previously come to the conclusion that my library must dynamically allocate objects(I may be wrong).
The program will only know at run time what number of objects will be created. Preferably I would like this number to be boundless but I know that will certainly cause me a problem with memory. Having said that the (dynamically allocated) objects will only be created at startup so a failure to allocate the memory will bring down the startup of the sketch.
I decided that to get around this I would limit the number of objects by allocating an array (e.g with size 10) and fill it up as the program determines the number of objects. Then I can stop filling up the array when I get to the last element. I could then experiment with the size of the array to see how many objects I can fit in at startup.
I am (relatively) new to c++ so I am still learning syntax and the way things are done. Of course improvement suggestions to my approach or otherwise are very welcome.