Skip to main content
deleted 7 characters in body
Source Link
KIIV
  • 4.9k
  • 1
  • 14
  • 23

You have to do it inside of function:

Sensor *sensors[2];

UltraSonicSensor uSensor;

void setup(){
  Serial.begin(9600);
  sensors[0] = &uSensor;
}

Or directly:

UltraSonicSensor uSensor;

Sensor *sensors[2]*sensors[] = {&uSensor, NULL};

Don't forget to make method Sense() virtual in the base class and in the inherited classes. Otherwise you'll have to also find out the type of the object and typecast it to correct class pointer before it's use.

class Sensor
{
  public:
    virtual int Sense() = 0; // pure virtual base class
};

// and inherited class:

class UltraSonicSensor: public Sensor {
  public:
    // ...

    // Prototype must be exactly the same, by using override
    // is ensured it actually overrides something from the parent class. 
    virtual int Sense() override {
        // ...
    }
    // ...
}

Also the last versions of Arduino IDE supports C++11 standard so you can use range based for loop:

for (Sensor * sensor : sensors) {
    int val = sensor->Sense();
    // ...
}

You have to do it inside of function:

Sensor *sensors[2];

UltraSonicSensor uSensor;

void setup(){
  Serial.begin(9600);
  sensors[0] = &uSensor;
}

Or directly:

UltraSonicSensor uSensor;

Sensor *sensors[2] = {&uSensor, NULL};

Don't forget to make method Sense() virtual in the base class and in the inherited classes. Otherwise you'll have to also find out the type of the object and typecast it to correct class pointer before it's use.

class Sensor
{
  public:
    virtual int Sense() = 0; // pure virtual base class
};

// and inherited class:

class UltraSonicSensor: public Sensor {
  public:
    // ...

    // Prototype must be exactly the same, by using override
    // is ensured it actually overrides something from the parent class. 
    virtual int Sense() override {
        // ...
    }
    // ...
}

Also the last versions of Arduino IDE supports C++11 standard so you can use range based for loop:

for (Sensor * sensor : sensors) {
    int val = sensor->Sense();
    // ...
}

You have to do it inside of function:

Sensor *sensors[2];

UltraSonicSensor uSensor;

void setup(){
  Serial.begin(9600);
  sensors[0] = &uSensor;
}

Or directly:

UltraSonicSensor uSensor;

Sensor *sensors[] = {&uSensor};

Don't forget to make method Sense() virtual in the base class and in the inherited classes. Otherwise you'll have to also find out the type of the object and typecast it to correct class pointer before it's use.

class Sensor
{
  public:
    virtual int Sense() = 0; // pure virtual base class
};

// and inherited class:

class UltraSonicSensor: public Sensor {
  public:
    // ...

    // Prototype must be exactly the same, by using override
    // is ensured it actually overrides something from the parent class. 
    virtual int Sense() override {
        // ...
    }
    // ...
}

Also the last versions of Arduino IDE supports C++11 standard so you can use range based for loop:

for (Sensor * sensor : sensors) {
    int val = sensor->Sense();
    // ...
}
Source Link
KIIV
  • 4.9k
  • 1
  • 14
  • 23

You have to do it inside of function:

Sensor *sensors[2];

UltraSonicSensor uSensor;

void setup(){
  Serial.begin(9600);
  sensors[0] = &uSensor;
}

Or directly:

UltraSonicSensor uSensor;

Sensor *sensors[2] = {&uSensor, NULL};

Don't forget to make method Sense() virtual in the base class and in the inherited classes. Otherwise you'll have to also find out the type of the object and typecast it to correct class pointer before it's use.

class Sensor
{
  public:
    virtual int Sense() = 0; // pure virtual base class
};

// and inherited class:

class UltraSonicSensor: public Sensor {
  public:
    // ...

    // Prototype must be exactly the same, by using override
    // is ensured it actually overrides something from the parent class. 
    virtual int Sense() override {
        // ...
    }
    // ...
}

Also the last versions of Arduino IDE supports C++11 standard so you can use range based for loop:

for (Sensor * sensor : sensors) {
    int val = sensor->Sense();
    // ...
}