Passing object array into object to store

tavalin: @mistergreen If I pass a pointer then I need to store the length (which isn't as bad as storing a whole new copy I guess) so I know how long the array is.

No you don't.

int array[] = {1,2,3,5};



void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  doStuff(array);
  
}

void loop() {
  // put your main code here, to run repeatedly:

}


void doStuff(int *in_array) {
     Serial.print(in_array[2]); //output - 3

}

You've hard coded the specific index. How does this approach work if I want to print each element in the array instead? (In my scenario I need to iterate through each device in the array.)

tavalin: You've hard coded the specific index. How does this approach work if I want to print each element in the array instead? (In my scenario I need to iterate through each device in the array.)

Ah, I see.

I usually do this.

int array[] = {1,2,3,5,null};

void doStuff(int *in_array) {
     int i = 0;
     while(in_array[i] != null) {
         Serial.print(in_array[i]);
         i++;
     }

}

mistergreen: Ah, I see.

I usually do this.

int array[] = {1,2,3,5,null};

void doStuff(int *in_array) {     int i = 0;     while(in_array[i] != null) {         Serial.print(in_array[i]);         i++;     }

}

What if you want to store the value 0 in the array? null is the same as 0.

In addition, if your array isn't ints, the compiler tells you that you can't compare a 'Device' to an 'int'.

I’m ideally looking for a solution where the rfm69_node class can access the user defined structure/memory locations for the Devices array and has access to the metadata like the size.

Which is what all the options do. The reason it didn’t work is you added your own bits, and left out the important bits.

Its not a choice, you need to remove the functions from the .cpp (get rid of it, tear it up, delete it)!

The <4> was from when I declared the object in the sketch. The <4> isn’t present in the .h or .cpp files.

Sorry I wrote 4, I meant N, you have added your own bits, and mixed two of my examples. Only required on functions defined outside the class definition.

Scrap the POD idea, its obviously just brought confusion. Which means you must have a reference or pointer inside the class not an object. What you had is wrong:

If you copy example three, and change it to a class, it is basically almost what you had.

//In header not CPP!

template< long N >
  class rfm69_node{
   
    public: 

      rfm69_node( Device (&devices)[N] );

    protected: 

      Device (&array)[N];
};

template< long N >
  rfm69_node<N>::rfm69_node( Device (&devices)[N] ) : array(devices) {
    //nothing
}
//Usage... (sketch)

Device uptimeDev(98, false, readUptime);
Device testDev(99, false, readUptime);
Device devArray [4] = {uptimeDev, testDev, testDev, uptimeDev};

rfm69_node< 4 > thing( devArray );

setup(){

}

loop(){

}

Anywhere in your class you can use ‘N’ for the count of elements.
Anywhere in your class you can use ‘devArray’ as an array.


As a side note, you need to fix up your Device class as you are creating three copies of each device, then a reference in the class.

uptimeDev, testDev are copied twice into the array… 3 copies of the same thing (i know repeating in the array is for testing)

You need to construct the objects in the Device array, or have an array of pointers to devices.

This can be left until you have everything else working though.

I've managed to get the reference and template way working and when I compile in the IDE it's pretty much the same size (give or take 5-10 bytes on program space and dynamic memory) compared to the pointer and array size variable way.

What were the other optimizations you were suggesting?