Instantiation

Hi guys,

I’m trying to get a better understanding of where I can Instantiate a object in a class I created, or better said what is the best practice. Whether before the class declaration, after declaration, or in the constructor. Also, do I Instantiate the object in .h, or .cpp file, if I’m trying to make it into a library?

Unless it's a singleton, the best practice is usually to instantiate classes in the application code. In a library, that would be in an example sketch.

You can have a look at how it was done for Serial (and Serial1, Serial2, Serial3) for example with extern déclaration in HardwareSerial.h

 #if defined(UBRRH) || defined(UBRR0H)
  extern HardwareSerial Serial;
  #define HAVE_HWSERIAL0
#endif
#if defined(UBRR1H)
  extern HardwareSerial Serial1;
  #define HAVE_HWSERIAL1
#endif
#if defined(UBRR2H)
  extern HardwareSerial Serial2;
  #define HAVE_HWSERIAL2
#endif
#if defined(UBRR3H)
  extern HardwareSerial Serial3;
  #define HAVE_HWSERIAL3
#endif