Reputation: 2439
How can I pass a QScopedPointer object to another function like that:
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]' is private
How can I solve it? Thanks!
Upvotes: 3
Views: 2906
Reputation: 98425
You can do it by accepting a reference to the pointer - that way you can swap the null local pointer with the one that was passed to you:
#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;
}
Output:
Constructed 0x7ff5e9c00220
Acting on 0x7ff5e9c00220
Destructed 0x7ff5e9c00220
foo has returned
Upvotes: 3