Should I implement all my data classes as QSharedData and use it with QSharedDataPointer?

I am starting Qt and I need to write some data classes. Would it be a good approach to write all these classes, as in the QSharedDataPointer example ( here ), or is it too much overhead (besides the fact more work)?

My classes in nature are very similar to the class below Employee . I will have to deal with several hundred copies, not ten thousand or millions.

The motivation for using QSharedData / QShareDataPointer would be to simplify manual memory management and have a common code style. But I'm still not sure that I am watching for some reservations.

From an example :

 class EmployeeData : public QSharedData { public: EmployeeData(); EmployeeData(const EmployeeData &other); ~EmployeeData(); int id; QString *name; }; class Employee { public: Employee(); Employee(int id, const QString &name); void setId(int id) { d->id = id; } void setName(const QString &name); int id() const { return d->id; } QString name() const; private: QSharedDataPointer<EmployeeData> d; }; 
+4
source share
1 answer

Benefits of running the QSharedDataPointer / pImpl idiom:

1) In some cases, you can avoid copying data (for example, if you have two or more identical Employee objects, they can share the same back-end EmployeeData, and not everyone who has a separate copy of the same data)

2) After adding additional data fields to the EmployeeData class, you only need to recompile the source code for the Employee class, because only the Employee class (presumably) ever accesses the EmployeeData class directly. All your other code (presumably) could access the Employee class, and therefore it would not have to be recompiled, because the size and layout of the Employee class would not change.

Both of these reasons can be convincing in the right circumstances - for example, Qt, the benefits of pImpl and implicit sharing in many places, because Qt needs to process large amounts of data extremely efficiently, and more importantly, because Qt’s relationship with third-party developers requires from him, ensure that new versions of the Qt shared library remain backward compatible with existing third-party application executable code that was compiled against older versions of Qt.

For your personal program, however, it is unlikely that you will see much benefit. If your data objects are as small / simple as shown in the EmployeeData example (or even if they are 10 or 100 times larger than this), the overhead of only copying the data is likely to be minimal ... and since you can, apparently recompiling your own code base whenever you need, the advantage of backward compatibility doesn't really matter to you.

So my advice would be to keep it simple and just do something in a standard way in C ++ by creating regular old copies of the instance instances of the default-copy-copy of your member objects when necessary (and, when possible, pass objects to methods through a const reference to minimize the number of copies). If you notice a measurable performance problem, at this point you can go back and rewrite some of your classes using pImpl / QSharedDataPointer to make sure that it gives you measurable speedup (although you will most likely find that this does not happen).

+5
source

All Articles