crimony:
Or use a reference member and an initialization list: ...
Yes, that gets rid of pointers, but I can't get it to compile:
Thermal.cpp: In constructor 'Thermal::Thermal(int, int)':
Thermal:8: error: member initializer expression list treated as compound expression
Thermal:8: error: invalid initialization of reference of type 'NewSoftSerial&' from expression of type 'int'
A bit of fiddling around to remove the error gives this:
#ifndef Thermal_h
#define Thermal_h
#include <NewSoftSerial.h>
#include <WProgram.h>
#include <WConstants.h>
class Thermal{
public:
Thermal(int RX_Pin, int TX_Pin);
NewSoftSerial _printer;
private:
int _RX_Pin;
int _TX_Pin;
};
#endif
That works, although I caution you that this is using a static constructor. Although it worked this time, static constructors can sometimes give weird results because of the order the constructors are called during program initialization.
My bad, I didn't test at all, and my C++ is very rusty.
The point was supposed to be that you need to use an initialization list if there is no default constructor for a member variable (in this case class NewSoftSerial).
That works, although I caution you that this is using a static constructor. Although it worked this time, static constructors can sometimes give weird results because of the order the constructors are called during program initialization.
I expect undefined behaviour if the constructor for NewSoftSerial is called in the global variable initialization stage.
Thermal.cpp.o: In function `loop':
Thermal.cpp:23: undefined reference to `__cxa_guard_acquire'
Thermal.cpp:23: undefined reference to `__cxa_guard_release'
Thermal.cpp:23: undefined reference to `atexit'
My expectations are unrealistic, as it turns out. Global scope is the right thing to do.
On closer inspection of the NewSoftSerial Library, it appears they must have ensured that they are immunized from the kind of initialisation issues covered here.