首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将QScopedPointer传递给函数

将QScopedPointer传递给函数
EN

Stack Overflow用户
提问于 2015-05-14 21:48:46
回答 1查看 2K关注 0票数 3

如何将QScopedPointer对象传递给另一个类似的函数:

代码语言:javascript
复制
bool addChild(QScopedPointer<TreeNodeInterface> content){
   TreeNode* node = new TreeNode(content);
}

TreeNode:

代码语言:javascript
复制
TreeNode::TreeNode(QScopedPointer<TreeNodeInterface> content)
{
    mContent.reset(content.take());
}

I get: error:'QScopedPointer::QScopedPointer(const QScopedPointer&) with T= TreeNodeInterface;Cleanup = QScopedPointerDeleter‘是私有的

我该怎么解决它呢?谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-05-14 23:38:16

您可以通过接受对指针的引用来完成此操作-这样您就可以将null本地指针与传递给您的指针交换:

代码语言:javascript
复制
#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;
}

输出:

代码语言:javascript
复制
Constructed 0x7ff5e9c00220 
Acting on 0x7ff5e9c00220 
Destructed 0x7ff5e9c00220 
foo has returned
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30239085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档