Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Why socket pointers are used everywhere instead of socket instances?
puzzled_giraffe last edited by
I'm new to C++/QT programming and I can't find an answer. I saw a lot of examples/questions/conversations and almost everywhere people use tcp socket pointers. If something goes wrong, users close socket, delete it and create another one.
Why not just use socket instance, close it, configure and try to connect it again with new parameters? Does it gives some unobvious profit to use pointers and recreate socket every time?
Can you be a little bit more specific where you see an issue here? Also note that QTcpSocket (and others) are derived from QObject which can not be copied.
VRonin last edited by
I'm new to C++
almost everywhere people use tcp socket pointers
Be aware that a lot of high-level programming languages use pointers almost everywhere and disguise them as instances instead. In C# this is the case for all
ref classes, in Java it is true for every object.
Why not just use socket instance, close it, configure and try to connect it again with new parameters?
You are perfectly free to do this. I think what most people do is to just pipe the "restart" case into the "start" workflow. The gain is a bit of code convenience at the expense of a bit of memory management overhead
Astrinus last edited by
If you want to
moveToThreadan object with instances instead of pointers, you will surely have some headaches.
Another use case is the pattern "new, connect and forget", not to have a member lying around in your class: in Qt 5 (yes, I am still working with Qt 4.8.5; no, I am not enjoying it) with lambdas you can have a very lean class declaration.