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. Using QSharedDataPointer with default copy constructor

Using QSharedDataPointer with default copy constructor

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

    When I look at the example for using QSharedDataPointer in the docs, I notice they implement a copy constructor, but no copy assignment operator, instead of using defaulted ones on both cases.

    Is there a special reason for that, i.e. will there be some issue with usage of QSharedDataPointer with defaulted copy constructor and copy assignment operator?

    SGaistS 1 Reply Last reply
    0
    • A Asperamanca

      When I look at the example for using QSharedDataPointer in the docs, I notice they implement a copy constructor, but no copy assignment operator, instead of using defaulted ones on both cases.

      Is there a special reason for that, i.e. will there be some issue with usage of QSharedDataPointer with defaulted copy constructor and copy assignment operator?

      SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      I am not sure I am following you. Do you mean this operator ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        I am not sure I am following you. Do you mean this operator ?

        A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        @SGaist
        The docs page to QSharedDataPointer (and I checked both 6.4.2 and 6.5dev) contains a little example class:

        #include <QSharedData>
        #include <QString>
        
        class EmployeeData : public QSharedData
        {
          public:
            EmployeeData() : id(-1) { }
            EmployeeData(const EmployeeData &other)
                : QSharedData(other), id(other.id), name(other.name) { }
            ~EmployeeData() { }
        
            int id;
            QString name;
        };
        
        class Employee
        {
          public:
            Employee() { d = new EmployeeData; }
            Employee(int id, const QString &name) {
                d = new EmployeeData;
                setId(id);
                setName(name);
            }
            Employee(const Employee &other)
                  : d (other.d)
            {
            }
            void setId(int id) { d->id = id; }
            void setName(const QString &name) { d->name = name; }
        
            int id() const { return d->id; }
            QString name() const { return d->name; }
        
          private:
            QSharedDataPointer<EmployeeData> d;
        };
        

        Two questions:

        1. Why not default the copy constructor of class Employee?
        Employee(const Employee &other) = default;
        
        1. Why not specify a copy assignment operator?
        Employee& operator=(const Employee& other) = default;
        

        I suspect this is just an old and half-baked example, but I want to make sure I'm not missing something here.

        JoeCFDJ 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          The class is old so at the time of writing, there was no such concept as default like there is now. Without forgetting than new features do not arrive at the same speed between compilers hence a pretty conservative approach is usually used.

          That said, with the actual compiler minimal requirements, you are likely correct in the sense that it can be modernized.

          You should open a ticket on the bug tracker for that if none already exists.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          A 1 Reply Last reply
          0
          • A Asperamanca

            @SGaist
            The docs page to QSharedDataPointer (and I checked both 6.4.2 and 6.5dev) contains a little example class:

            #include <QSharedData>
            #include <QString>
            
            class EmployeeData : public QSharedData
            {
              public:
                EmployeeData() : id(-1) { }
                EmployeeData(const EmployeeData &other)
                    : QSharedData(other), id(other.id), name(other.name) { }
                ~EmployeeData() { }
            
                int id;
                QString name;
            };
            
            class Employee
            {
              public:
                Employee() { d = new EmployeeData; }
                Employee(int id, const QString &name) {
                    d = new EmployeeData;
                    setId(id);
                    setName(name);
                }
                Employee(const Employee &other)
                      : d (other.d)
                {
                }
                void setId(int id) { d->id = id; }
                void setName(const QString &name) { d->name = name; }
            
                int id() const { return d->id; }
                QString name() const { return d->name; }
            
              private:
                QSharedDataPointer<EmployeeData> d;
            };
            

            Two questions:

            1. Why not default the copy constructor of class Employee?
            Employee(const Employee &other) = default;
            
            1. Why not specify a copy assignment operator?
            Employee& operator=(const Employee& other) = default;
            

            I suspect this is just an old and half-baked example, but I want to make sure I'm not missing something here.

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • SGaistS SGaist

              The class is old so at the time of writing, there was no such concept as default like there is now. Without forgetting than new features do not arrive at the same speed between compilers hence a pretty conservative approach is usually used.

              That said, with the actual compiler minimal requirements, you are likely correct in the sense that it can be modernized.

              You should open a ticket on the bug tracker for that if none already exists.

              A Offline
              A Offline
              Asperamanca
              wrote on last edited by
              #6

              @SGaist
              Done

              1 Reply Last reply
              1
              • A Asperamanca has marked this topic as solved on

              • Login

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