首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >QList of QScopedPointers

QList of QScopedPointers
EN

Stack Overflow用户
提问于 2016-01-13 08:06:41
回答 1查看 3.6K关注 0票数 5

我试图将QScopedPointers存储在QList中。

我发现这句话

还可以使用QList >。-库巴·奥伯1月14日下午6:17

(关于这个答案的第一个评论:https://stackoverflow.com/a/21120575/3095014)

这篇文章https://forum.qt.io/topic/59338/solved-qlist-of-qscopedpointers,这意味着这应该是可行的。但是,如果我试图编译第二个链接的代码,就会得到以下错误:

代码语言:javascript
复制
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(404) : error C2248: 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer' : cannot access private member declared in class 'QScopedPointer<Label,QScopedPointerDeleter<T>>'
    with
    [
        T=Label
    ]
    E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qscopedpointer.h(170) : see declaration of 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer'
    with
    [
        T=Label
    ]
    E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(403) : while compiling class template member function 'void QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::node_construct(QList<QScopedPointer<T,QScopedPointerDeleter<T>>>::Node *,const QScopedPointer<T,QScopedPointerDeleter<T>> &)'
    with
    [
        T=Label
    ]
    E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(553) : see reference to function template instantiation 'void QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::node_construct(QList<QScopedPointer<T,QScopedPointerDeleter<T>>>::Node *,const QScopedPointer<T,QScopedPointerDeleter<T>> &)' being compiled
    with
    [
        T=Label
    ]
    E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(794) : while compiling class template member function 'QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::~QList(void)'
    with
    [
        T=Label
    ]
    ..\tableview_row_dnd\main.cpp(13) : see reference to function template instantiation 'QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>::~QList(void)' being compiled
    with
    [
        T=Label
    ]
    ..\tableview_row_dnd\main.cpp(20) : see reference to class template instantiation 'QList<QScopedPointer<Label,QScopedPointerDeleter<T>>>' being compiled
    with
    [
        T=Label
    ]
E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(405) : error C2248: 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer' : cannot access private member declared in class 'QScopedPointer<Label,QScopedPointerDeleter<T>>'
    with
    [
        T=Label
    ]
    E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qscopedpointer.h(170) : see declaration of 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer'
    with
    [
        T=Label
    ]

为什么这不适合我?

EN

回答 1

Stack Overflow用户

发布于 2016-01-13 09:34:22

存储在Qt容器中的值应该具有可分配的数据类型。这意味着它们应该有一个默认构造函数、一个复制构造函数和一个赋值操作符。

QScopedPointer禁用了它的复制构造函数和赋值操作符。您不能为对方分配两个指针,但可以使用QScopedPointer::resetQScopedPointer::swapQScopedPointer::take显式地传递底层原始指针的所有权。

在某一时刻、移动构造函数和移动赋值运算符被添加到QScopedPointer中。新的移动语义使得这成为可能:

代码语言:javascript
复制
QList<QScopedPointer<Label>> mLabels;
mLabels.append(QScopedPointer<Label>(new Label));

在这里,向列表添加一个临时值,并使用移动构造函数创建新的列表项。

后来他们恢复了这一改变:

在QScopedPointer中添加一个移动构造器是没有意义的,因为移动意味着“逃离范围”,这破坏了QScopedPointer的基本点。

如果您确实希望有一个智能指针列表,可以使用可分配的QSharedPointer或支持移动语义的std::unique_ptr

如果您谈到跟踪QObjects子类的生存期,特别是小部件,我建议使用Qt子父机制,而不是智能指针。

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34761327

复制
相关文章

相似问题

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