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. QTcpServer copy constructor
Forum Update on Monday, May 27th 2025

QTcpServer copy constructor

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 655 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.
  • N Offline
    N Offline
    nevdokimof
    wrote on last edited by
    #1

    Why the copy constructor of QTcpServer is private?

    So instead of this:

    //server.h
    
    class Server : public QObject
    {
        Q_OBJECT
    
    private:
        QTcpServer tcpServer;
    
    public:
        explicit Server(QObject *parent = nullptr);
    };
    
    //server.cpp
    
    Server::Server(QObject *parent) : QObject(parent)
    {
        tcpServer = QTcpServer();
    }
    

    I am forced to use pointer to an object, like this:

    //server.h
    
    class Server : public QObject
    {
        Q_OBJECT
    
    private:
        QTcpServer* tcpServer;
    
    public:
        explicit Server(QObject *parent = nullptr);
    };
    
    //server.cpp
    
    Server::Server(QObject *parent) : QObject(parent)
    {
        tcpServer = new QTcpServer();
    }
    

    I believe it is not memory safe, am I wrong?

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

      You just have to make sure to delete the pointer when it is no longer needed (e.g. in the dtor).
      Or pass a valid parent so it gets automatically deleted when the parent is deleted - see http://doc.qt.io/qt-5/objecttrees.html

      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
      5
      • N Offline
        N Offline
        nevdokimof
        wrote on last edited by
        #3

        Oh, so I can set this (in my case) as a parent of my tcpServer and it will be automatically destroyed with the Server destruction?

        Seems a bit tricky

        mrjjM 1 Reply Last reply
        0
        • N nevdokimof

          Oh, so I can set this (in my case) as a parent of my tcpServer and it will be automatically destroyed with the Server destruction?

          Seems a bit tricky

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @nevdokimof
          Whole Qt is designed about this system.
          Its works really well and free you from the burden of deleting all the widgets manually.
          The reason the copy constructor is disabled is that you are not allowed to clone most Qt objects
          due to identity and the signal and slot system.
          Its all explained in the link mr @Christian-Ehrlicher shows.

          1 Reply Last reply
          4
          • VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            Hi, just a side note, it is perfectly valid to allocate a QObject on the stack.

            class Server : public QObject
            {
                Q_OBJECT
            
            private:
                QTcpServer tcpServer;
            
            public:
                explicit Server(QObject *parent = nullptr);
            };
            Server::Server(QObject *parent) : QObject(parent){}
            

            is perfectly fine.
            What you are having trouble with is tcpServer = QTcpServer(); which is unnecessary in C++. If you need to pass arguments to the constructors of your QObjects you can do it via the initializer list

            "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
            3

            • Login

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