Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

having to reassign the values of a array of objects to work in c++/arduino

i have code that have a global array of object, when i create it i already assign the objects, but before start using it on the code i have to reassign the values of the array on the function setup so i can access the objects inside it

the code that create the vector

HX711 CELL1;
HX711 CELL2;
HX711 CELL3;
HX711 CELL4;
HX711 celulas[] = {CELL1, CELL2, CELL3, CELL4};       // vector with the objects

the code on setup function that call the function that initialize the object, inside there isnt any new or anything, the begin is a normal function

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  CELL1.begin(7, 8);
  celulas[0] = CELL1;
  CELL2.begin(5, 6);
  celulas[1] = CELL2;
  CELL3.begin(0, 1);
  celulas[2] = CELL3;
  CELL4.begin(A0, A1);
  celulas[3] = CELL4;

Can anyone explain what is going on ?

i expected that i can use the array after i created and assigned, not having to reassign the values of it

>Solution :

You can of course use the array, but celulas[0] is not the same object as CELL1.

The line

HX711 celulas[] = {CELL1, CELL2, CELL3, CELL4};

copies the objects into the array – you now have eight HX711s, all independent from each other.

And, since your objects are default-initialized, it is almost equivalent to

HX711 celulas[4];

but with more copying.

In your setup, use the array elements directly:

celulas[0].begin(7, 8);
celulas[1].begin(5, 6);
celulas[2].begin(0, 1);
celulas[3].begin(A0, A1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading