array of objects and scope

Hi,

I would like to try some polymorphism on arduino. I know that’s not a really good idea and not very ideal on microprocesseur but I would like to know what’s wrong in my code.

I would like to create some objects and ask them to do their stuff in the loop. The pc tells the arduino what to do so the kind of object may be different.

I’m from java and not very kind into pointers and all :frowning:

I tried some things with new, malloc and so on but nothings works. Does anubody can tell me a way to correct this ? I don’t even need to free memory but the creation of object could change.

My example is a sample of Animals, with children class like Dog and Cat ( Chien et Chat ) and I ask them to talk

class Animal
{
  protected :
  int _num ;
  
  public :
  Animal(){}
  Animal( int num )
  {
    _num = num ;
  }

  virtual void parle(){} ; // methode virtuelle pure a redefinir dans les sous classes
};

class Chien :
public Animal
{
  public :
  
  Chien( int num ):
  Animal( num )
  {
  }
  
  virtual void parle()
  {
    Serial.println("wouarf");
  }
};

class Chat :
public Animal
{
  public :
  
  Chat( int num ):
  Animal( num )
  {
  }
  
  virtual void parle()
  {
    Serial.println("miaou");
  }
};

Animal * betes = new Animal[20] ;
int nbBetes = 0 ;

void setup()
{
  Serial.begin(9600);
    
  betes[nbBetes++] = new Chat(1);
  betes[nbBetes++] = new Chien(2); 
}

void loop()
{
  
  for( int i = 0 ; i < nbBetes ; i++ )
  {
    betes[i]->parle();
    Serial.println( millis() );
    delay( 1000 ); 
  }  
}

You can not use the new operator with arduino (but you can, google it).

Try this for demonstration of polymorphy: [CODE UNTESTED]

class Animal
{
  protected :
  int _num ;

  public :
  Animal(){}
  Animal( int num )
  {
    _num = num ;
  }

  virtual void parle(){} ; // methode virtuelle pure a redefinir dans les sous classes
};

class Chien :
public Animal
{
  public :

  Chien( int num ):
  Animal( num )
  {
  }

  virtual void parle()
  {
    Serial.println("wouarf");
  }
};

class Chat :
public Animal
{
  public :

  Chat( int num ):
  Animal( num )
  {
  }

  virtual void parle()
  {
    Serial.println("miaou");
  }
};

Animal c1 = Chien(1);
Animal c2 = Chat(2);

Animal * poly;

int nbBetes = 0 ;

void setup()
{
  Serial.begin(9600);
  
  poly = &c1;
  poly->parle();
  
  poly = &c2;
  poly->parle();
}

void loop()
{
  //nothing
}

Hi,
Thanks to your help I made this code who looks ok

class Animal
{
  protected :
  int _num ;

  public :
  Animal(){}
  Animal( int num )
  {
    _num = num ;
  }

  virtual void parle() const=0 ;
};

class Chien :
public Animal
{
  public :

  Chien( int num ):
  Animal( num )
  {
  }

  virtual void parle() const
  {
    Serial.println("wouarf");
  }
};

class Chat :
public Animal
{
  public :

  Chat( int num ):
  Animal( num )
  {
  }

  virtual void parle() const
  {
    Serial.println("miaou");
  }
};

Animal * poly[20];

int nbBetes = 0 ;

Chien c1 = Chien(1);
Chat c2 = Chat(2);

void setup()
{
  Serial.begin(9600);

  poly[nbBetes++] = &c1;
  poly[nbBetes++] = &c2;
}

void loop()
{
  Serial.println( millis() );
  for( int i = 0 ; i < nbBetes ; i++ )
  {
    poly[i]->parle();
    delay( 1000 );
  }  
}

I had to change the

Animal c1 = Chien(1); by Chien c1 = Chien(1);

But I have another probleme.

The part

(.....)

Animal * poly[20];

int nbBetes = 0 ;

Chien c1 = Chien(1);
Chat c2 = Chat(2);

void setup()
{
  Serial.begin(9600);

  poly[nbBetes++] = &c1;
  poly[nbBetes++] = &c2;
} 
(.....)

but if I want to move the declaration of the object into a function and change it by

Animal * poly[20];

int nbBetes = 0 ;

void setup()
{
  Serial.begin(9600);
  
  Chien c1 = Chien(1);
  Chat c2 = Chat(2);

  poly[nbBetes++] = &c1;
  poly[nbBetes++] = &c2;
}

The arduino is not ok and reboot. Witch way should I use malloc() with this code ?

void setup()
{
  Serial.begin(9600);

  Chien c1 = Chien(1);
  Chat c2 = Chat(2);

  poly[nbBetes++] = &c1;
  poly[nbBetes++] = &c2;
}

This will not work for because of something (you probably know) called scope. The data for c1 and c2 will only 'live'/be valid as long as you're in the setup(), that is, the objects are destructed on exiting the function in which they were declared.

Animal * poly[20];

int nbBetes = 0 ;

void setup()
{
  Serial.begin(9600);

  Chien c1 = Chien(1);
  Chat c2 = Chat(2);

  poly[nbBetes++] = &c1;
  poly[nbBetes++] = &c2;
}

When declared in setup, c1 and c2 are local variables. They go out of scope at the end of setup. The memory that they used is freed.

When you try to reference that memory, using the pointer to it that is stored in the poly array, it fails, because the pointer is no longer valid.

Thanks. That make sense but i thougt that there was a way to do it like that ...

Here is my new fail …

class Animal
{
protected :
  int _num ;

public :
  Animal(){
  }
  Animal( int num )
  {
    _num = num ;
  }

  virtual void parle() const=0 ;
};

class Chien :
public Animal
{
public :

  Chien( int num ):
  Animal( num )
  {
  }

  virtual void parle() const
  {
    Serial.println("wouarf");
  }
};

class Chat :
public Animal
{
public :

  Chat( int num ):
  Animal( num )
  {
  }

  virtual void parle() const
  {
    Serial.println("miaou");
  }
};

class Menagerie
{
private :
  int _nbBetes ;
  Animal * poly[20];

public :
  Menagerie()
  {
    _nbBetes = 0 ;
  }

  void addChat(int num)
  {
    poly[_nbBetes++] = &Chat(num);
  }
  void addChien( int num )
  {
    poly[_nbBetes++] = &Chien(num);
  }

  void faitParler()
  {
    for( int i = 0 ; i < _nbBetes ; i++ )
    {
      poly[i]->parle();
      delay( 1000 );
    }
  }
};

Menagerie mesAnimaux = Menagerie() ;

void setup()
{
  Serial.begin(9600);
}

int etat = 0 ;

void loop()
{
  Serial.println( millis() );

  switch( etat )
  {
  case 0 :

    mesAnimaux.addChat(3);
    mesAnimaux.addChien(7);
    
    etat++ ;
    break ;
  case 1 :

    mesAnimaux.faitParler();

    break ;
  }


}