2013-09-30 11 views
8

私は公にQWidgetから継承されたクラスがあります。コピーコンストラクタ

class MyWidget : public QWidget 
{ 
    Q_OBJECT 
public: 
    MyWidget(const MyWidget& other) 
     : 
    obj1(other.obj1), 
    obj2(other.obj2) 

private: 
    some_class obj1; 
    some_class obj2; 
}; 

私は私のプロジェクトを構築し、コンパイラは文句を:

WARNING:: Base class "class QWidget" should be explicitly initialized in the copy constructor.

私は上の他の質問からチェックアウト

私の答えを得た。 しかし、私はこのようなことの初期化を追加したとき、実際には、次のとおりです。

QWidget::QWidget(const QWidget&) is private within this context

だから、私が間違っているのものを私に説明してください:私はコンパイルエラーだ

class MyWidget : public QWidget 
{ 
    Q_OBJECT 
public: 
    MyWidget(const MyWidget& other) 
     : 
    QWidget(other), //I added the missing initialization of Base class 
    obj1(other.obj1), 
    obj2(other.obj2) 

private: 
    some_class obj1; 
    some_class obj2; 
}; 

+5

'QWidget'があなたの派生型のどちらかであってはならないことを意味する、構成可能コピーするように設計されていないようです:QtはコピーがそのdocumentationではQObjectに許可された場合に生じるであろう問題のいくつかの例を示します。 – juanchopanza

+0

'QWidget'のコピーコンストラクタを明示的に作成したのでしょうか、それともコンパイラに残しましたか? – Olayinka

+0

QWidgetのコピーコンストラクタを作成する必要はありません。私は、オブジェクトのコピーコンストラクタのinitリストのQWidget :: Cpoy-Constructorを呼び出すオブジェクトを初期化することができます。 –

答えて

16

QObject Class説明ページが伝え:

QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.

QObjectを設計することにより、非コピー可能であるので、あなたは、QTオブジェクトをコピーすることになっていないことを意味します。

最初の警告は、基本クラス(QWidget)の初期化を指示します。これを行うには、新しい基本オブジェクトを作成するつもりです。それがあなたがしたいことではないかと思います。

第2のエラーは、私が上に書いたことです:qtオブジェクトをコピーしないでください。

9

すべてのQtクラスは、QObjectから派生することによってコピーできません。

多態性オブジェクトのコピーなどの特定の値意味操作を禁止するのが一般的です。

A Qt Object...

  • might have a unique QObject::objectName(). If we copy a Qt Object, what name should we give the copy?
  • has a location in an object hierarchy. If we copy a Qt Object, where should the copy be located?
  • can be connected to other Qt Objects to emit signals to them or to receive signals emitted by them. If we copy a Qt Object, how should we transfer these connections to the copy?
  • can have new properties added to it at runtime that are not declared in the C++ class. If we copy a Qt Object, should the copy include the properties that were added to the original?
関連する問題