Using QSharedDataPointer with default copy constructor
-
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?
-
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?
-
@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:
- Why not default the copy constructor of class Employee?
Employee(const Employee &other) = default;
- 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.
-
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.
-
@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:
- Why not default the copy constructor of class Employee?
Employee(const Employee &other) = default;
- 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.
-
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.
-