我如何将qscopedpointer对象传递到这样的另一个函数:
bool addChild(QScopedPointer<TreeNodeInterface> content){
TreeNode* node = new TreeNode(content);
}
treenode:
TreeNode::TreeNode(QScopedPointer<TreeNodeInterface> content)
{
mContent.reset(content.take());
}
我得到:错误:'QSCOPEDPOINTER :: QSCOPEDPOINTER(const qscopedPointer&amp;)[带有t = treenodeInterface;清理= qscopedpointerdeleter]'是私人
我该如何解决?谢谢!
您可以接受对指针的引用 - 这样,您可以将NULL本地指针与传递给您的指针交换:
#include <QScopedPointer>
#include <QDebug>
class T {
Q_DISABLE_COPY(T)
public:
T() { qDebug() << "Constructed" << this; }
~T() { qDebug() << "Destructed" << this; }
void act() { qDebug() << "Acting on" << this; }
};
void foo(QScopedPointer<T> & p)
{
using std::swap;
QScopedPointer<T> local;
swap(local, p);
local->act();
}
int main()
{
QScopedPointer<T> p(new T);
foo(p);
qDebug() << "foo has returned";
return 0;
}
输出:
Constructed 0x7ff5e9c00220
Acting on 0x7ff5e9c00220
Destructed 0x7ff5e9c00220
foo has returned