Skip to main content
added 57 characters in body
Source Link
Yank
  • 231
  • 1
  • 3
  • 10

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++){
    animalArray[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 ..

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++){
    animalArray[i].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 ..

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 ..

added 214 characters in body
Source Link
Yank
  • 231
  • 1
  • 3
  • 10

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(){
  dogfor (int i=0;i<3;i++){
    animalArray[i].alive = true;
  }
  //here when I print dog'seach animal's alive flag it'sis true'true'
}

void loop(){
  for (int i=0;i<3;i++){
         // here the alive flag of dogeach animal is still false  , itthey 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 ..

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


void setup(){
  dog.alive = true;
  //here when I print dog's alive flag it's true
}

void loop(){
  // here the alive flag of dog is still false  , it should be 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 ..

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++){
    animalArray[i].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 ..

Source Link
Yank
  • 231
  • 1
  • 3
  • 10

How can I use a 'Global' class object?

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


void setup(){
  dog.alive = true;
  //here when I print dog's alive flag it's true
}

void loop(){
  // here the alive flag of dog is still false  , it should be 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 ..