2016-01-13 26 views
2

我試圖將QScopedPointers存儲在QList中。QScopedPointers QList

我發現這條評論

你也可以使用的QList>。 - 庫巴奧伯1月14日在'14 18:17

(第一對這個答案的評論:https://stackoverflow.com/a/21120575/3095014

這個帖子https://forum.qt.io/topic/59338/solved-qlist-of-qscopedpointers這意味着這應該工作和。但是,如果我嘗試編譯第二個鏈接的代碼,我得到這個錯誤:

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 
    ] 

爲什麼這不適合我?

+0

顯示您的代碼的相關部分。 – hank

+0

正如我寫的:這是在這個帖子https://forum.qt.io/topic/59338/solved-qlist-of-qscopedpointers(沒有富功能)的確切代碼 – avb

回答

8

存儲在Qt容器中的值應該是可指定的數據類型。這意味着他們應該有一個默認構造函數,一個拷貝構造函數和一個賦值運算符。

QScopedPointer已禁用其複製構造函數和賦值運算符。您不能將兩個指針分配給對方,但可以使用QScopedPointer::reset,QScopedPointer::swapQScopedPointer::take明確地轉讓基礎原始指針的所有權。

At some point移動構造函數和移動賦值運算符被添加到QScopedPointer。新的移動語義使之成爲可能:

QList<QScopedPointer<Label>> mLabels; 
mLabels.append(QScopedPointer<Label>(new Label)); 

這裏臨時值添加到列表和新列表項使用移動構造函數創建。

Later他們恢復了這一變化:

添加此舉構造器來QScopedPointer是沒有意義的,因爲 移動手段「逃避範圍」,打破QScopedPointer的根本點 。

如果你真的想擁有智能指針的列表,你可以使用QSharedPointer這是轉讓或std::unique_ptr支持移動語義。

如果你談論跟蹤QObjects子類特別是小部件的生命週期,我會建議使用Qt子父機制而不是智能指針。

+0

請注意,可分配的要求與STL容器相比,數據類型非常不理想。在一些情況下(例如:默認可施工性),需求來自實施不善。解決方法:如果您確實需要'unique_ptr'的矢量,請使用STL容器而不是Qt容器。 – peppe