Doc: Modernize QSharedDataPointer Example for Rule of Compliance

Replaced the manually defined copy constructor with = default to
improve clarity and maintainability.Added a default copy assignment
operator to ensure consistency,as copy constructor and copy
assignment should be defined as a pair.Also included defaulted move
constructor and move assignment operator in accordance with the rule
of 6,ensuring the example is modern,complete,and suitable for
educational purposes.

Fixes: QTBUG-111392
Change-Id: Ia7cfccfc46182d8d9102868fc93dc0ebd9649a32
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
This commit is contained in:
Dheerendra Purohit 2025-06-06 16:47:02 +05:30
parent 045eb2179c
commit b52c0dafd8

View File

@ -6,15 +6,16 @@
//! [0] //! [0]
#include <QSharedData> #include <QSharedData>
#include <QSharedDataPointer>
#include <QString> #include <QString>
class EmployeeData : public QSharedData class EmployeeData : public QSharedData
{ {
public: public:
EmployeeData() : id(-1) { } EmployeeData() : id(-1) {}
EmployeeData(const EmployeeData &other) EmployeeData(const EmployeeData &other)
: QSharedData(other), id(other.id), name(other.name) { } : QSharedData(other), id(other.id), name(other.name) {}
~EmployeeData() { } ~EmployeeData() = default;
int id; int id;
QString name; QString name;
@ -22,34 +23,38 @@ class EmployeeData : public QSharedData
class Employee class Employee
{ {
public: public:
//! [1] //! [1]
Employee() { d = new EmployeeData; } Employee() { d = new EmployeeData; }
//! [1] //! [2] //! [1] //! [2]
Employee(int id, const QString &name) { Employee(int id, const QString &name) {
d = new EmployeeData; d = new EmployeeData;
setId(id); setId(id);
setName(name); setName(name);
} }
//! [2] //! [7] //! [2] //! [7]
Employee(const Employee &other) Employee(const Employee &other) = default;
: d (other.d) Employee &operator=(const Employee &other) = default;
{
} Employee(Employee &&other) = default;
//! [7] Employee &operator=(Employee &&other) = default;
//! [3]
~Employee() = default;
//! [7]
//! [3]
void setId(int id) { d->id = id; } void setId(int id) { d->id = id; }
//! [3] //! [4] //! [3] //! [4]
void setName(const QString &name) { d->name = name; } void setName(const QString &name) { d->name = name; }
//! [4] //! [4]
//! [5] //! [5]
int id() const { return d->id; } int id() const { return d->id; }
//! [5] //! [6] //! [5] //! [6]
QString name() const { return d->name; } QString name() const { return d->name; }
//! [6] //! [6]
private: private:
QSharedDataPointer<EmployeeData> d; QSharedDataPointer<EmployeeData> d;
}; };
//! [0] //! [0]