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. Can a class deriving from QSharedData use the default copy constructor?
Forum Updated to NodeBB v4.3 + New Features

Can a class deriving from QSharedData use the default copy constructor?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.4k 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.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    In the example in Qt docs, a copy constructor is explicitly defined for a trivial class with only copyable members.

    Is this intentional? Or is it fine to declare the copy constructor as "=default"?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      As long as the QSharedData copy constructor is called (which should be guaranteed by the standard) =default works just as well

      "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
      2
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        In the example, Employee and EmployeeData are defined in the same header file. But now imagine you split the two parts and you have:

        employee.h

        #ifndef EMPLOYEE_H
        #define EMPLOYEE_H
        
        #include <QString>
        #include <QSharedDataPointer>
        
        class EmployeeData;
        
        class Employee
        {
          public:
            Employee();
            Employee(int id, const QString &name);
            Employee(const Employee &other);
            ~Employee();
        
            int id() const;
            void setId(int id);
        
            QString name() const;
            void setName(const QString &name);
        
        private:
            QSharedDataPointer<EmployeeData> d;
        };
        
        #endif // EMPLOYEE_H
        

        Now the compiler couldn't generate the copy constructor as the member QSharedDataPointer<EmployeeData> d is not fully known. So in this case, the usual case I'd say, you actually have to define / implement the (trivial) copy-constructor.

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

          I think we are talking about two different things.
          Did you mean to =default the copy constructor for EmployeeData or the one for Employee?

          "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
          2
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Oh. If you were talking about EmployeeData, yes, you can reduce that code to:

            struct EmployeeData : public QSharedData
            {
                int id = -1;
                QString name;
            };
            
            1 Reply Last reply
            2
            • A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              Yes, I was talking about the private data class. Since all I was doing was faithfully copy every single member, "=default" deemed a viable alternative.

              I have tried it and so far have not run into any issues. Together with your input, I feel confident this is safe to do so, and the example looks as it does just because it was written pre-C++11

              Thanks!

              1 Reply Last reply
              0

              • Login

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