I am writing some code for the Arduino IDE and running into an issue where a reference, that is a class member, seems to lose/change its value across function calls.
MyStream is defined as an extern in MyStream.cpp and inherits from Stream.
In code below, the references seem to be equal in the constructor but not in foo()! Can't figure out what's going on
StreamProvider.h
class StreamProvider {
private:
Stream& stream;
public:
StreamProvider(Stream&);
size_t foo(const char*);
};
StreamProvider.cpp
StreamProvider::StreamProvider(Stream& s)
: stream(s)
{
Serial.printf("\nMyStream %x", &MyStream);
Serial.printf("\nStream %x", &stream);
}
size_t StreamProvider::foo(const char* buf) {
Serial.printf("\nMyStream %x", &MyStream);
Serial.printf("\nStream %x", &stream);
}
MyStream.h
class _MyStream: public Stream {
size_t write(uint8_t);
int available();
int read();
int peek();
};
extern _MyStream MyStream;
MyStream.cpp
size_t _MyStream::write(uint8_t) {
return 0;
}
int _MyStream::available() {
return 0;
}
int _MyStream::read() {
return 0;
}
int _MyStream::peek() {
return 0;
}
_MyStream MyStream;
Output
MyStream 3ffee340 /** ctor**/
Stream 3ffee340 /** ctor**/
MyStream 3ffee340 /** foo**/
Stream 3fffff6c ??? /** foo**/
I have tried to look around and this seems unexplainable! Of course nothing is, but then I am not sure if I am missing something here...Any help is appreciated.
Thanks