user8643: I created a class inside the sketch file(not in another file) and init it as object before setup() and loop() as a global object, after I change some parameters inside the object in setup() , I found in loop() the parameters in the global object remain unchanged.
Here is the pseudocode code to describe my question:
//all of the code below are in the same ino file.
class Animal{
public:
  bool alive = false; 
  Animal (bool liveStatus){
    alive = liveStatus;
  }
}

Animal dog(false);
Animal cat(false);
Animal lion(false);
Animal animalArray(dog,cat,lion);




void setup(){

  for (int i=0;i<3;i++){
    Animal current_animal = animalArray[i];
    current_animal.alive = true;
  }
  //here when I print each animal's alive flag is 'true'
}

void loop(){
  for (int i=0;i<3;i++){
         // here the alive flag of each animal is still false  , they should be all true!!
  }

}

It looks like after I quit setup() somehow every change of the global object inside the setup are all gone .
Thanks for helping me .. 

user1585: Your problem here is that Animal current_animal = animalArray[i]; is making a whole new Animal object that starts as a copy of animalArray[i];, but after that the two are no longer associated. Then when you call current_animal.alive = true;, you are not actually changing the animal in animalArray[i] anymore.
The simplest way to fix this is to make current_animal a reference to the object you pick out of the array, although a pointer would work the same way. With this code:
Animal& current_animal = animalArray[i];
current_animal.alive = true;

current_animal becomes a reference to animalArray[i], instead of a copy. Then operating on current_animal changes the same data that is stored in animalArray[i] like you intend.

8643: <p>I created a class inside the sketch file(not in another file) and init it as object before setup() and loop() as a global object, after I change some parameters inside the object in setup() , I found in loop() the parameters in the global object remain unchanged.
Here is the pseudocode code to describe my question:</p>

<pre><code>//all of the code below are in the same ino file.
class Animal{
public:
  bool alive = false; 
  Animal (bool liveStatus){
    alive = liveStatus;
  }
}

Animal dog(false);
Animal cat(false);
Animal lion(false);
Animal animalArray(dog,cat,lion);




void setup(){

  for (int i=0;i&lt;3;i++){
    Animal current_animal = animalArray[i];
    current_animal.alive = true;
  }
  //here when I print each animal's alive flag is 'true'
}

void loop(){
  for (int i=0;i&lt;3;i++){
         // here the alive flag of each animal is still false  , they should be all true!!
  }

}
</code></pre>

<p>It looks like after I quit setup() somehow every change of the global object inside the setup are all gone .</p>

<p>Thanks for helping me .. </p>
 1585: <p>Your problem here is that <code>Animal current_animal = animalArray[i];</code> is making a whole new Animal object that starts as a copy of <code>animalArray[i];</code>, but after that the two are no longer associated. Then when you call <code>current_animal.alive = true;</code>, you are not actually changing the animal in <code>animalArray[i]</code> anymore.</p>

<p>The simplest way to fix this is to make <code>current_animal</code> a reference to the object you pick out of the array, although a pointer would work the same way. With this code:</p>

<pre><code>Animal&amp; current_animal = animalArray[i];
current_animal.alive = true;
</code></pre>

<p><code>current_animal</code> becomes a reference to <code>animalArray[i]</code>, instead of a copy. Then operating on <code>current_animal</code> changes the same data that is stored in <code>animalArray[i]</code> like you intend.</p>
