[英]QSharedDataPointer with forward-declared class
Qt文档建议将QSharedDataPointer与劣质的可见实现结合使用并不常见 。
因此,根据文档中的一个小示例,我提出了以下来源(SSCCE)。
该接口很简单,只是私有类和handle类的前向声明,其中声明了copy-ctor和d-tor:
#include <QtCore/QSharedDataPointer>
class ModelPrivate;
class Model {
public:
Model();
Model(const Model &other);
~Model();
QSharedDataPointer<ModelPrivate> d;
};
只需声明并定义劣等类。
#include <QSharedData>
class ModelPrivate:
public QSharedData {
public:
};
包含c-tors / d-tor的实现,取自文档。
#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() {
}
凡事都失败了。
#include <QString>
#include "Model.h"
int main(int argc, char *argv[]) {
QString s1, s2;
s2 = s1;
Model m1, m2;
m2 = m1;
}
只需两个实例和一个赋值,就像其他任何共享类一样。 但是,由于
invalid use of incomplete type 'class ModelPrivate'
我无法根据文档确定如何使这项工作按预期的方式进行,即也没有在标头中完全声明私有类。 我知道这样做是可行的,但我想了解这些文档。 文档中也包含分配共享类的示例。 从上面链接的文档中:
这里不是严格要求复制构造函数,因为EmployeeData类与Employee(employee.h)类包含在同一文件中。 但是,通常不将QSharedData的私有子类与包含QSharedDataPointer的公共类包含在同一文件中。 通常,这种想法是通过将QSharedData的私有子类放置在单独的文件中来向用户隐藏,该文件不会包含在公共文件中。 在这种情况下,我们通常将EmployeeData类放在单独的文件中,该文件不会包含在employee.h中。 取而代之的是,我们只需要在employee.h中预先声明私有子类EmployeeData:
我想编译在分配Model
时使用的operator=
处失败。
您的概念存在几个主要问题:
与原始想法不同,您确实需要在单独的文件中拥有私有类才能做到这一点。
不管您使用的是文档中原始示例的概念,还是没有使用。 您将复制构造函数概念更改为复制约束。 自然,您需要分别重新实现该运算符。
这是我为官方示例重写的工作示例,因为我认为最好为后代自定义该示例,而不是与分散的示例更内联以更好地理解上游:
#include "employee.h"
int main()
{
Employee e1(1001, "Albrecht Durer");
Employee e2 = e1;
Emplyoee e3;
e3 = e2;
e1.setName("Hans Holbein");
}
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <QSharedDataPointer>
#include <QString>
class EmployeeData;
class Employee
{
public:
Employee();
Employee(int id, QString name);
Employee(const Employee &other);
Employee& operator =(const Employee &other);
~Employee();
void setId(int id);
void setName(QString name);
int id() const;
QString name() const;
private:
QSharedDataPointer<EmployeeData> d;
};
#endif
#ifndef EMPLOYEE_P_H
#define EMPLOYEE_P_H
#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;
};
#endif
#include "employee.h"
#include "employee_p.h"
Employee::Employee()
{
d = new EmployeeData;
}
Employee::Employee(int id, QString name)
{
d = new EmployeeData;
setId(id);
setName(name);
}
Employee::Employee(const Employee &other)
: d (other.d)
{
}
Employee& Employee::operator =(const Employee &other)
{
d = other.d;
return *this;
}
Employee::~Employee()
{
}
void Employee::setId(int id)
{
d->id = id;
}
void Employee::setName(QString name)
{
d->name = name;
}
int Employee::id() const
{
return d->id;
}
QString Employee::name() const
{
return d->name;
}
TEMPLATE = app
TARGET = main
QT = core
HEADERS += employee.h employee_p.h
SOURCES += main.cpp employee.cpp
问题实际上是通过QSharedDataPointer
类中的模板声明和定义的d
成员的赋值运算符。
该解决方案很简单,就像将共享类的赋值运算符移动到模块中一样:
#include <QtCore/QSharedDataPointer>
class ModelPrivate;
class Model {
public:
Model();
Model(const Model &other);
Model &operator =(const Model &other); /* <-- */
~Model();
QSharedDataPointer<ModelPrivate> d;
};
#include <QSharedData>
class ModelPrivate:
public QSharedData {
public:
};
#include "Model.h"
#include "Model_p.h"
Model::Model():
d(new ModelPrivate()) {
}
Model::Model(const Model &other):
d(other.d) {
}
Model::~Model() {
}
Model &Model::operator =(const Model &other) {
d = other.d;
return *this;
}
#include <QString>
#include "Model.h"
int main() {
QString s1, s2;
s2 = s1;
Model m1, m2;
m2 = m1;
}
TEMPLATE = app
TARGET = example
QT = core
HEADERS += Model.h Model_p.h
SOURCES += Model.cc main.cc
实际上,这就是Qt Universe本身中其他类的实现方式。 例如,考虑QTextCursor
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.