Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why socket pointers are used everywhere instead of socket instances?

Why socket pointers are used everywhere instead of socket instances?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 388 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • puzzled_giraffeP Offline
    puzzled_giraffeP Offline
    puzzled_giraffe
    wrote on last edited by
    #1

    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?

    how did I get here?

    VRoninV 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • puzzled_giraffeP puzzled_giraffe

        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?

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @puzzled_giraffe said in Why socket pointers are used everywhere instead of socket instances?:

        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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4
        • A Offline
          A Offline
          Astrinus
          wrote on last edited by
          #4

          If you want to moveToThread an 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.

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved