Go Down

Topic: Accessing array objects by reference out of array (Read 934 times) previous topic - next topic

andrew_k

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: [Select]

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.

RuggedCircuits

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

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

Code: [Select]
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: [Select]
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

andrew_k

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: [Select]

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

}

InvalidApple





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: [Select]
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 -->



andrew_k

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

InvalidApple

Hey there;

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

:)

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy