How to have an object array as class member?

Desired outcome have a class (board) that has a member attribute (sensorlist) that is an array of sensor objects

why I wants it So then I can have a standard library and load this on multiple boards with varying only a few lines on the main sketch file

and that I could have:

  • board level options (e.g. board.batterycheck() )
  • or sensorlist level options like board.countsensors() (counting elements in sensorlist) = {Sensor("distanceSensor",A0,myboard.boardId,0)};
  • or sensor level operations like board.sensorlist[0].turnonSensor
class Sensor{
    int boardId;
    String sensorName;
    int sensorId;
    int Pin;
    //sensor name, pin, boardid
    Sensor(String, int, int);

Sensor::Sensor(String name, int _pin, int _boardId)
        sensorName = name;
        Pin = _pin;  //signal out pin of sensor
                boardId = _boardId;


#include "sensor.h"

class Board{
    int boardId;
        Sensor sensorList[];

Then I would like to be able to instantiate a board object

Board myboard = Board(1,"prototypeUno");

and then add sensor objects the the array of myboard


my problem is that as soon as I add this, the board object fails to instantiate because I have parameters on the sensor class constructor.

is there any way to achieve this?

A fully managed list like this somehow implies using dynamic memory (or own more-advanced cpp memory pooling). Generally speaking, using dynamic memory usually is a bad idea for embedded systems, unless you exactly know what you are doing.

Try using statically defined global objects for your sensors and just store pointers to them for convenience.
The attached hardware is usually pre-known anyway.

class Board {
  Sensor** sensorList[10];

  Board() { memset(&sensorList, 0, sizeof(sensorList)); }

Board myboard = Board();
Sensor s = Sensor("description", A0);

myboard.sensorList[0] = &s;