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 20 Feb 2023, 10:16 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?

    S 1 Reply Last reply 20 Feb 2023, 20:11
    0
    • A Asperamanca
      20 Feb 2023, 10:16

      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?

      S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Feb 2023, 20:11 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 21 Feb 2023, 08:22
      0
      • S SGaist
        20 Feb 2023, 20:11

        Hi,

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

        A Offline
        A Offline
        Asperamanca
        wrote on 21 Feb 2023, 08:22 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.

        J 1 Reply Last reply 21 Feb 2023, 21:54
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 21 Feb 2023, 21:14 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 22 Feb 2023, 08:20
          0
          • A Asperamanca
            21 Feb 2023, 08:22

            @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.

            J Offline
            J Offline
            JoeCFD
            wrote on 21 Feb 2023, 21:54 last edited by
            #5
            This post is deleted!
            1 Reply Last reply
            0
            • S SGaist
              21 Feb 2023, 21:14

              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 22 Feb 2023, 08:20 last edited by
              #6

              @SGaist
              Done

              1 Reply Last reply
              1
              • A Asperamanca has marked this topic as solved on 22 Feb 2023, 08:20

              1/6

              20 Feb 2023, 10:16

              • Login

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