如何将QScopedPointer对象传递给另一个类似的函数:
bool addChild(QScopedPointer<TreeNodeInterface> content){
TreeNode* node = new TreeNode(content);
}TreeNode:
TreeNode::TreeNode(QScopedPointer<TreeNodeInterface> content)
{
mContent.reset(content.take());
}I get: error:'QScopedPointer::QScopedPointer(const QScopedPointer&) with T= TreeNodeInterface;Cleanup = QScopedPointerDeleter‘是私有的
我该怎么解决它呢?谢谢!
发布于 2015-05-14 23:38:16
您可以通过接受对指针的引用来完成此操作-这样您就可以将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 returnedhttps://stackoverflow.com/questions/30239085
复制相似问题