Accessing array objects by reference out of array

My title doesn't do a good job of explaining what I'm trying to do, which has likely hindered my ability to find a solution by searching.

I have a group of object instances, which are also in an array. I wish to access these instances by reference both while iterating through the array and individually.
This code is a simplified version of my problem:

struct Foo
{
 int bar; 
};

Foo Foo1;
Foo Foo2;
Foo Foo3;

Foo FooArray[3] = {Foo1, Foo2, Foo3};

void setup()
{
 Serial.begin(9600);
 Foo1.bar = 1;
 Foo2.bar = 2;
 Foo3.bar = 3; 
}


void loop() 
{
 for(byte i=0; i<3; i++)
 {
   FooArray[i].bar += 1; 
 }
 
 for(byte i=0; i<3; i++)
 {
  Serial.println(FooArray[i].bar); 
 }
 
 Serial.println(Foo1.bar); 
 Serial.println(Foo2.bar); 
 Serial.println(Foo3.bar); 
 
 delay(2000);

}

What I would like to achieve is that the FooArray*.bar += 1; also affects the bar property of each instance when accessed directly.*

Well, there are two approaches, structs or classes. Your code declares Foo as a struct and proceeds to use it as a class :slight_smile:

So let's look at both possibilites, first as structures:

struct Foo
{
 int bar;
};

struct Foo Foo1;
struct Foo Foo2;
struct Foo Foo3;

struct Foo *FooArray[3] = {&Foo1, &Foo2, &Foo3};

void setup()
{
 Serial.begin(9600);
 Foo1.bar = 1;
 Foo2.bar = 2;
 Foo3.bar = 3;
}


void loop()
{
 for(byte i=0; i<3; i++)
 {
   FooArray[i]->bar += 1;
 }

 for(byte i=0; i<3; i++)
 {
  Serial.println(FooArray[i]->bar);
 }

 Serial.println(Foo1.bar);
 Serial.println(Foo2.bar);
 Serial.println(Foo3.bar);

 delay(2000);

}

Now with Foo as a class:

class Foo
{
public:
 int bar;
};

Foo Foo1;
Foo Foo2;
Foo Foo3;

Foo *FooArray[3] = {&Foo1, &Foo2, &Foo3};

void setup()
{
 Serial.begin(9600);
 Foo1.bar = 1;
 Foo2.bar = 2;
 Foo3.bar = 3;
}


void loop()
{
 for(byte i=0; i<3; i++)
 {
   FooArray[i]->bar += 1;
 }

 for(byte i=0; i<3; i++)
 {
  Serial.println(FooArray[i]->bar);
 }

 Serial.println(Foo1.bar);
 Serial.println(Foo2.bar);
 Serial.println(Foo3.bar);

 delay(2000);

}

--
Check out our new shield: http://www.ruggedcircuits.com/html/gadget_shield.html

Thank you!

This is my first time working in C++ and I'm clearly struggling with the syntax. My use of structs was an attempt to simplify my example; my code uses classes form a library I'm writing.

Your example gave me the building blocks to achieve what I'm really trying to do, which is more accurately represented by the following example:

class Foo
{
public:
 int bar;
};

Foo Foo1;
Foo Foo2;
Foo Foo3;

Foo *FooArray[3] = {&Foo1, &Foo2, &Foo3};


void barPlus(Foo &f)
{
  f.bar += f.bar; 
}

void setup()
{
 Serial.begin(9600);
 Foo1.bar = 1;
 Foo2.bar = 2;
 Foo3.bar = 3;
}


void loop()
{
 for(byte i=0; i<3; i++)
 {
   barPlus(*FooArray[i]);
 }

 for(byte i=0; i<3; i++)
 {
  Serial.println(FooArray[i]->bar);
 }

 Serial.println(Foo1.bar);
 Serial.println(Foo2.bar);
 Serial.println(Foo3.bar);

 delay(2000);

}

Hey there.

This is my first time working in C++ and I'm clearly struggling with the syntax. My use of structs was an attempt to simplify my example

You may be surprised to know that in C++ the only difference between structs and classes is that in a class, anything is by default is declared as "private"- That's it!

References are good to know: However, I don't think that you are using them right. A reference is an automatic "call by reference", so that you don't have to have a list of pointers.

(See Code to the right)

class Foo
{
public:
 int bar;
};

Foo FooArray[3];

//use Foo at address...
void barPlus(Foo &f) 
{
  f.bar += f.bar;
}

void setup()
{
 Serial.begin(9600);
 FooArray[0].bar = 1;
 FooArray[1].bar = 2;
 FooArray[2].bar = 3;
}


void loop()
{
 for(byte i=0; i<3; i++)
 {
   //Passes address of class
   // (&FooArray[i])
   barPlus(FooArray[i]);
 }

 for(byte i=0; i<3; i++)
 {
  Serial.println(FooArray[i].bar);
 }

 delay(2000);

}

InvalidApple, your version appears cleaner, but it loses one of the original requirements; I need to address each instance by its own variable name, not just by its array index.

p.s. hi from Melbourne :wink:

Hey there;

I'd then say to just use pointers. (opinion as fact...)

:slight_smile: