For the first time I'm trying to get my feet seriously wet with C++ classes. Parts of the code below were just beg, steal and borrow
It was (for me) quite tricky to pass an array as a parameter to the constructor.
I have the following on an include file.
Signal.h
#ifndef _SIGNAL_H_
#define _SIGNAL_H_
#include <Arduino.h>
const uint8_t numPins = 8;
/*
Base class for signals
*/
class Signal
{
protected:
uint8_t (&_pins)[numPins];
uint8_t _value;
// initialize use a reference to an array
Signal(uint8_t (&pins)[numPins]) : _pins(pins) {};
// initialize use a pointer to an array
Signal(uint8_t (*pins)[numPins]) : _pins(*pins) {};
public:
// begin method is shared in derived classes
void begin();
// this method differs in the derived classes
virtual void show(uint8_t value);
// shared with derived classes
void printPins();
};
class SignalSimple : public Signal
{
public:
// initialize use a reference to an array
SignalSimple(uint8_t (&pins)[numPins]) : Signal{&pins} {};
// initialize use a pointer to an array
SignalSimple(uint8_t (*pins)[numPins]) : Signal{*pins} {};
void show(uint8_t value);
};
class SignalComplex : public Signal
{
public:
// initialize use a reference to an array
SignalComplex(uint8_t (&pins)[numPins]) : Signal{&pins} {};
// initialize use a pointer to an array
SignalComplex(uint8_t (*pins)[numPins]) : Signal{*pins} {};
void show(uint8_t value);
};
#endif
The question is about the use of the colon. I understand that in e.g. class SignalComplex : public Signal the colon indicates that SignalComplex is derived from Signal. But I do not know what the colon does in the constructor Signal(uint8_t (&pins)[numPins]) : _pins(pins) {};.
To complete the info
Signal.cpp
#include "Signal.h"
// begin method is shared with derived classes
void Signal::begin()
{
for (uint8_t cnt = 0; cnt < sizeof(_pins); cnt++)
{
if (_pins[cnt] != 255)
{
pinMode(_pins[cnt], OUTPUT);
}
}
}
void Signal::printPins()
{
for (uint8_t cnt = 0; cnt < sizeof(_pins); cnt++)
{
Serial.print(_pins[cnt]);
Serial.print(",");
}
Serial.println();
}
void SignalSimple::show(uint8_t value)
{
_value = value;
Serial.print("setting _value in SignalSimple to ");
Serial.println(value);
}
void SignalComplex::show(uint8_t value)
{
_value = value;
Serial.print("setting _value in SignalComplex to ");
Serial.println(value);
}
And the sketch; it prints out the pins as I expect, no surprises there ( except for that it works
)
/*
This is the first serious attempt at classes !!
For passing an array to a constructor, code based on https://stackoverflow.com/questions/9426932/how-do-i-pass-an-array-to-a-constructor
For using derived classes and passing an array to the constructor, code based on https://www.learncpp.com/cpp-tutorial/constructors-and-initialization-of-derived-classes/
*/
#include "Signal.h"
uint8_t pinsSimple[numPins] = {2, 3, 4, 255, 255, 255, 255, 255};
SignalSimple sbs(pinsSimple);
uint8_t pinsComplex[numPins] = {8, 9, 19, 11, 12, 13, 255, 255};
SignalComplex sbc(pinsComplex);
void setup()
{
Serial.begin(57600);
while (!Serial);
sbs.printPins();
sbs.begin();
sbs.show(3);
sbc.printPins();
sbc.begin();
sbc.show(45);
}
void loop()
{
}
Thanks in advance