Go Down

Topic: Storing Objects of different Types onto an Array? (Read 528 times) previous topic - next topic

mistergreen

Hi,
I plan to have different classes with similar methods, say "loop()".
I then want to make instances of the classes and store them in an array so I can call loop() of all the objects.

something like:
Code: [Select]

Tiger *objArray = [10];

objArray[0] = new Tiger();
//the problem is this
objectArray[1] = new Dog();
objectArray[2] = new Penguin();

void loop() {
   for(int i =0; i < 3; i++) {
       objectArray->loop();
   }

}



So as you can see the array is type Tiger. How can I include Dog and Penguin?

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

Example:

Code: [Select]

class animal
  {
  public: 
  virtual void loop () = 0;   // pure function
  };
 
class Tiger : public animal
  {
  public:
  void loop () { Serial.println ("Tiger."); }
  };

class Dog : public animal
  {
  public:
  void loop () { Serial.println ("Dog."); }
  };

class Penguin : public animal
  {
  public:
  void loop () { Serial.println ("Penguin."); }
  };

animal * objArray [3];
 
void setup ()
  {
  Serial.begin (115200);
  objArray [0] = new Tiger;
  objArray [1] = new Dog;
  objArray [2] = new Penguin;
  }  // end of setup

void loop ()
  {
  for (int i = 0; i < 3; i++)
    objArray [i]->loop ();
  delay (1000);
  }  // end of loop


Output:

Code: [Select]

Tiger.
Dog.
Penguin.
Tiger.
Dog.
Penguin.
Tiger.
Dog.
Penguin.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

mistergreen

Thanks!
I had a feeling that was the answer like in any other language and was looking around how to sub-class in C/Arduino.

mistergreen

Oh by the way, is there a destructor function that is called when I delete?

like

delete objArray[0];

do I need a destructor in the class

Code: [Select]

void desctructor() {
     //not sure if I need to do this
     delete super;

}


I come from Objective C and we have a dealloc function in every class that cleans up after itself.

Nick Gammon

This is C++ by the way, not some "Arduino language".

If you plan to use a destructor to clean up then the destructors must be virtual, including the base class.

Modified example:

Code: [Select]

class animal
  {
  public: 
  virtual void loop () = 0; 
  virtual ~animal () { Serial.println ("animal destructor."); }
  };
 
class Tiger : public animal
  {
  public:
  void loop () { Serial.println ("Tiger."); }
  virtual ~Tiger () { Serial.println ("Tiger destructor."); }
  };

class Dog : public animal
  {
  public:
  void loop () { Serial.println ("Dog."); }
  virtual ~Dog () { Serial.println ("Dog destructor."); }
  };

class Penguin : public animal
  {
  public:
  void loop () { Serial.println ("Penguin."); }
  virtual ~Penguin () { Serial.println ("Penguin destructor."); }
  };

animal * objArray [3];
 
void setup ()
  {
  Serial.begin (115200);
  Serial.println ();
 
  objArray [0] = new Tiger;
  objArray [1] = new Dog;
  objArray [2] = new Penguin;

  for (int i = 0; i < 3; i++)
    objArray [i]->loop ();

  Serial.println ("Deleting objects ...");

  for (int i = 0; i < 3; i++)
     delete objArray [i];
  }  // end of setup

void loop () { }


Output:

Code: [Select]

Tiger.
Dog.
Penguin.
Deleting objects ...
Tiger destructor.
animal destructor.
Dog destructor.
animal destructor.
Penguin destructor.
animal destructor.


Be warned that there is a problem with free (and hence delete) in versions of the IDE up to 1.0.3.

You may need to install the fix described here:  Fixing String Crashes
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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