如何将QScopedPointer对象传递给另一个函数:
bool addChild(QScopedPointer<TreeNodeInterface> content){
TreeNode* node = new TreeNode(content);
}
树节点:
TreeNode::TreeNode(QScopedPointer<TreeNodeInterface> content)
{
mContent.reset(content.take());
}
我得到: 错误:&#39; QScopedPointer :: QScopedPointer(const QScopedPointer&amp;)[with T = TreeNodeInterface;清理= QScopedPointerDeleter]&#39;是私人的
我该如何解决?谢谢!
答案 0 :(得分:1)
你可以通过接受对指针的引用来实现它 - 这样你可以将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