use of pre-instanciated object in lib or not

Hi,

I noticed that most arduino libs define a class and also instanciate an object of this class:
Lib.h:
class LibClass {. };
extern LibClass Lib;
lib.cpp:
LibClass Lib;

Examples:
NTPClient, TwoWire, ...

Question 1: I understand this is done as a way to simplify the declaration (user can use the pre-instanciated object), and possibly to share one global variable potentially used by other libraries ?

is this correct ?

Question 2: can it be a problem if I instanciate another object, instead of using the one from lib ?

Question 3: if I instanciate and use another object, will the compiler remove the memory used by the other instanciated object from library ?

I did not easily find information on those topics, but I appreciate any pointer.

laurent_martin:
is this correct ?

It's all about making the libraries more beginner friendly. A beginner finds that instantiation code very confusing. We often get posts here on the forum from people who did a copy past of a fragment of a program using an object from a library without also adding the instantiation code and then can't figure out why they get a "was not declared in this scope" error.

It makes sense to do this in the cases where it's likely the user will only need a single instance. In other cases (e.g., Servo) the user is likely to need multiple instances. In this case, you are likely to find that the global object approach is not done.

Sometime there are reasons for there to only ever be one instance of an object class. This is called a "singleton" and, if the class is written correctly, there is no way to create a second object of that class.

It makes sense for the library to create an instance only if there are no arguments to the constructor.

You could always just use a #define if you want to use a different name for the pre-instantiated object:

#define MyInstanceName Lib