2016-09-01 18 views
1

QObjectから派生したクラスにQScopedPointerを使用します。これは、しかし、失敗したコンストラクタでQScopedpointerがQ_DISABLE_COPYで失敗する

private: 
    QScopedPointer<CalculationManager> calculationManager; 

qscopedpointer.hのラインで

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    calculationManager(QScopedPointer<CalculationManager>(new CalculationManager())) 

を:として

MainWindowは、それを含む

Q_DISABLE_COPY(QScopedPointer) 

付:

C:\Qt\5.7\mingw53_32\include/QtCore/qscopedpointer.h: In constructor 'MainWindow::MainWindow(QWidget*)': 
C:\Qt\5.7\mingw53_32\include/QtCore/qscopedpointer.h:176:20: error: 'QScopedPointer<T, Cleanup>::QScopedPointer(const QScopedPointer<T, Cleanup>&) [with T = CalculationManager; Cleanup = QScopedPointerDeleter<CalculationManager>]' is private 
    Q_DISABLE_COPY(QScopedPointer) 
        ^

コピーはなぜ、どこで行われますか?

答えて

3

QScopedPointerには移動コンストラクタがありません。そのための
、あなたはこの行を検討している場合:ここで

calculationManager(QScopedPointer<CalculationManager>(new CalculationManager())) 

をタイプQScopedPointer<CalculationManager>の一時的なオブジェクトが構築され、その後、calculationManagerデータメンバにコピーされます。
あなたは余分なコピーを避けるために、この操作を行うことができます。

calculationManager(new CalculationManager()) 

それはCalculationManagerへのポインタを期待コンストラクタを呼び出しますし、何のコピーは行われません。

関連する問題