11 years, 3 months ago.

EthernetInterface singleton

Hi All,

Is the EthernetInterface a singleton class? If it is, shouldn't there be a private constructer? If I create two EthernetInterface objects, the compiler still gives the successful result.

Thanks

1 Answer

11 years, 3 months ago.

Hi Ye, currently, the EthernetInterface is just a "namespace" container for a set of static methods.

You need a Singleton only when a class and its fields need to be allocated only once and you want its methods to point to the same instance.

EthernetInterface does not have fields to be allocated.

When you allocate an EthernetInterface, you basically get a single pointer to the same set of static methods, that is basically the same principle of a Singleton, without the processing "fat".

Of course, the API could have been a set of C functions, but we wanted to use an API that in the future could be modified more easily to a system with multiple Ethernet interfaces (in that case, they will be proper instances).

Cheers, Emilio

Accepted Answer

Thanks Emilio. This sounds reasonable. But I still have a question. I'm wrapping some C functions into a C++ class, like what you have done on mbed library. All functions in C are global functions, so should this class be a singleton class, or could be a normal class? Even the functions are handling hardware? BTW, do you have any suggestion on how to wrap a C library into a C++ library?

posted by Ye Cheng 09 Jan 2013

This "comment" should be moved to a separate question. The answer is too broad to be contained in a "comment".

posted by Emilio Monti 09 Jan 2013