Make Serial unique reference

Hi,

I am using Serial.begin(9600) in my setup's sketch and I am using another class instance.

FOO myFoo = FOO();

I would like to print some informations from myFoo instance.

Do I have to pass pointer of Serial object to myFoo instance or can I just declare

Serial.begin(9600) into my FOO class constructor ? Thank you

Serial.begin(9600) into my FOO class constructor ?

A bad idea.

Do I have to pass pointer of Serial object to myFoo instance or can I just declare

A good idea. I suggest adding a "begin" method to FOO that you can use to pass in Serial.

You can leave Serial.begin() in your setup() and just call Serial.whatever() from in your other class because Serial is a global instance of the HardwareSerial class. Have a look in the library's HardwareSerial.h and HardwareSerial.cpp files to see how it gets set up for your board.

You can just call Serial.whatever() from in your other class

But not in global constructors.

But not in global constructors.

Agreed.

While I'm here... this syntax is bad:

FOO myFoo = FOO();

All sorts of implicit operations happen here that you probably were not expecting. In C++ you should just do this to create an object using the default constructor:

FOO myFoo;