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?
Forum Updated to NodeBB v4.3 + New Features

Why socket pointers are used everywhere instead of socket instances?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 419 Views 2 Watching
  • 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.
  • P Offline
    P Offline
    puzzled_giraffe
    wrote on 6 Mar 2020, 14:47 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?

    V 1 Reply Last reply 6 Mar 2020, 15:43
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 6 Mar 2020, 14:51 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
      • P puzzled_giraffe
        6 Mar 2020, 14:47

        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?

        V Offline
        V Offline
        VRonin
        wrote on 6 Mar 2020, 15:43 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 6 Mar 2020, 16:09 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

          1/4

          6 Mar 2020, 14:47

          • Login

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