The Qt documentation suggests that using the QSharedDataPointer with a visible implementation of its inferior is not typical.
So according to the small example snipped in the docs, I came up with the following source (SSCCE).
The interface: Model.h
The interface is straight forward, just the forward declaration of the private class and the handle class, with copy-ctor and d-tor declared:
#include <QtCore/QSharedDataPointer>
class ModelPrivate;
class Model {
public:
Model();
Model(const Model &other);
~Model();
QSharedDataPointer<ModelPrivate> d;
};
Private header: Model_p.h
Just declares and defines the inferior class.
#include <QSharedData>
class ModelPrivate:
public QSharedData {
public:
};
Implementation: Model.cc
Consisting of the implementation of the c-tors/d-tor, taken from the docs.
#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() {
}
Use case: main.cc
Where it all 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, as one would do with any other shared class as well. However, it fails badly because of
invalid use of incomplete type 'class ModelPrivate'
I can't figure out how to make this work the expected way according to the documentation, i.e. without fully declaring the private class in the header as well. I know it works when doing so but I'd like to understand the docs. The example of assigning shared classes is included in the docs as well. From the above-linked docs:
The copy constructor is not strictly required here, because class EmployeeData is included in the same file as class Employee (employee.h). However, including the private subclass of QSharedData in the same file as the public class containing the QSharedDataPointer is not typical. Normally, the idea is to hide the private subclass of QSharedData from the user by putting it in a separate file which would not be included in the public file. In this case, we would normally put class EmployeeData in a separate file, which would not be included in employee.h. Instead, we would just predeclare the private subclass EmployeeData in employee.h this way:
I suppose compilation fails at the operator=
that is used when assigning the Model
.