我很确定这是不可能的,我需要第二个初始化函数调用......但也许是这样?
。主要的
auto obj = QSharedPointer<myObject>(new myObject());
H。
class someObject;
class myObject : public QEnableSharedFromThis<myObject> {
QSharedPointer<someObject> child;
public:
myObject();
~myObject();
};
class someObject {
QSharedPointer<myObject> obj;
public:
someObject(QSharedPointer<myObject> parent);
~someObject();
};
.cpp
myObject::myObject() {
child = QSharedPointer<someObject>(new someObject(sharedFromThis()));
}
myObject::~myObject() {
}
someObject::someObject(QSharedPointer<myObject> parent) {
parent = parent;
}
someObject::~someObject() {
}
当检查子对象时,这将返回一个无效的父对象。我想这是因为我们在共享它之前正在制作 sharedFromThis() ... any1 可以提出另一种方法而不使用新函数来将父对象设置为子对象吗?