Pages: [1]   Go Down
Author Topic: Accessing array objects by reference out of array  (Read 698 times)
0 Members and 2 Guests are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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.
Logged

0
Offline Offline
Faraday Member
**
Karma: 13
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
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:
Code:
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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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);

}
Logged

Melbourne, Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 219
Have you tried turning it off and on again?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there.

Quote
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)
Code:
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);

}
<!-- Tested code -->

Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-wink
Logged

Melbourne, Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 219
Have you tried turning it off and on again?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey there;

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

smiley
Logged

Pages: [1]   Go Up
Jump to: