I am learning C++ and I am trying to use my knowledge of programming other languages to understand C++, which appears to be confusing me a lot. I am working on a basic socket program and trying to figure out the best way to handle creation of the socket class so I can read/write and only connect once.
In my other languages I would create a static object class that would allow me to reference it, if it wasn't created I would create the socket and connect. If it was created I would just return it for referencing.
But a class can't be a static class (at least that's what I've read) so I fall back on the next option I know which is Singleton.
So I ended up with something like
class Socket{
static Socket* socket;
public:
Socket& Get()
{
if (!socket) socket = new Socket;
return *socket;
}
};
And I would have my starting/connecting stuff in the constructor. But is this how it is suppose to be done? There seems to be a lot of conflicting stuff on the internet. For example use people use mutex, and some people use templates.
Which way would be best for something like a socket wrapping class?