The Qt documentation suggests that using a QSharedDataPointer with a visible implementation of its bottom is not typical.
So, in accordance with a small example captured in the docs, I came up with the following source (SSCCE).
Interface: Model.h
The interface is straightforward, only direct declaration of the private class and descriptor class with the declaration of copy-ctor and d-tor:
#include <QtCore/QSharedDataPointer> class ModelPrivate; class Model { public: Model(); Model(const Model &other); ~Model(); QSharedDataPointer<ModelPrivate> d; };
Private Title: Model_p.h
Just declares and defines the class below.
#include <QSharedData> class ModelPrivate: public QSharedData { public: };
Implementation: Model.cc
It consists of the implementation of c-tors / d-tor, taken from the documents.
#include "Model.h" #include "Model_p.h" class ModelPrivate: public QSharedData { }; Model::Model(): d(new ModelPrivate()) { } Model::Model(const Model &other): d(other.d) { } Model::~Model() { }
Example usage: main.cc
Where all this failed.
#include <QString> #include "Model.h" int main(int argc, char *argv[]) { QString s1, s2; s2 = s1; Model m1, m2; m2 = m1; }
Just two instances and an assignment, like any other general class. However, it does not work well due to
invalid use of incomplete type 'class ModelPrivate'
I cannot figure out how to make this work in the expected way according to the documentation, i.e. without a full private class declaration in the title. I know this works in doing this, but I would like to understand the documents. An example of assigning common classes is also included in the documents. From the above documents:
The copy constructor is not strictly required here, because the EmployeeData class is included in the same file as the Employee class (Employee.h). However, including the private subclass of QSharedData in the same file as the public class containing QSharedDataPointer is not typical. The usual idea is to hide a private subclass of QSharedData from the user by placing it in a separate file that is not included in the public file. In this case, we usually put the EmployeeData class in a separate file that will not be included in employee.h. Instead, we simply provided a private subclass of EmployeeData in employee.h as follows:
I assume that compilation failed with operator= , which is used when assigning Model .