vshymanskyy / TinyGSM

A small Arduino library for GSM modules, that just works
GNU Lesser General Public License v3.0
1.93k stars 715 forks source link

TinyGSM object understanding #521

Open krupis opened 3 years ago

krupis commented 3 years ago

Hello. I am learning about creating custom libraries and from what I have seen, every library creates a object which then can be using with different functions from a class. I have been digging through the TinyGSM library trying to find a TinyGSM object and I believe it is being initialised here:

 public:
  explicit TinyGsmSim800(Stream& stream) : stream(stream) {
    memset(sockets, 0, sizeof(sockets));
  }

However, could someone give explain me why it is being initialised in this way? I would expect it to look like? (Note the steram replaced with serial)

explicit TinyGsmSim800(Stream& stream) : serial(stream) {

Also, what is memset(sockets,0,sizeof(sockets)); used for? What are these sockets for?

SRGDamia1 commented 3 years ago

There are a lot of helpful guides to C++ on the internet. You should definitely read those; the things you're asking about are standard C++, nothing terribly special.

Anyway, the library doesn't create objects, it defines classes of objects. You create the object of that class in your program. The extra colon in the constructor is to initialize the inner stream object of the TinyGSMSim800 object when the later is created. The memset function is just setting aside chunk of memory for the clients (sockets) which are expected to be created to use with the modem object.

If you really want to dive into the guts of this library, it is built using the Curiously recurring template pattern to allow inheritance while minimizing memory use. That's probably not something you'd find in a beginner level C++ guide, but you shouldn't have any trouble finding tutorials on it.