在QObject派生类中重复Q_DISABLE_COPY

时间:2021-01-24 18:44:09

In Qt there is a macro that allows declaring private copy constructurs and assignment operators for classes: http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#Q_DISABLE_COPY

在Qt中,有一个宏允许为类声明私有复制构造函数和赋值操作符:http://qt-project.org/doc/qt-5.0/qtcore/qobject.html#Q_DISABLE_COPY

It is said that this macro should be used for all QObject (especially QWidget) derived classes.

据说这个宏应该用于所有QObject(特别是QWidget)派生类。

I understand how this works and why it is useful.

我理解它是如何工作的,为什么它有用。

What I do not understand: Is there any reason to repeat Q_DISABLE_COPY in my QObject derived classes while QObject already contains Q_DISABLE_COPY and through this effectively prevents my derived classes from being copied?

我不理解的是:是否有理由在QObject派生类中重复Q_DISABLE_COPY,而QObject已经包含Q_DISABLE_COPY,并通过此有效地防止我的派生类被复制?

1 个解决方案

#1


11  

The error message that might be printed from attempting to copy the derived class might refer to QObject instead of your code, so the error might appear to be confusing. For example, using Visual Studio 2012 to compile this code

试图复制派生类时可能打印的错误消息可能指向QObject而不是您的代码,因此错误可能看起来很混乱。例如,使用Visual Studio 2012来编译这段代码。

class MyClass : public QObject
{

};

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(application);

    MyClass obj1;
    MyClass obj2(obj1);

    QApplication app(argc, argv);
    app.setOrganizationName("QtProject");
    app.setApplicationName("Application Example");
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

results in this error (and a bunch of references to QObject).

导致这个错误(以及对QObject的一系列引用)。

error: C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject'

错误:C2248:“QObject: QObject”:无法访问类“QObject”中声明的私有成员

Changing MyClass to

改变MyClass

class MyClass : public QObject
{
private:
    Q_DISABLE_COPY(MyClass)
public:
    MyClass();
};

results in a much more user friendly group of errors that refer to MyClass, starting with

结果会出现更多的用户友好的错误,这些错误都指向MyClass,首先是。

error: C2248: 'MyClass::MyClass' : cannot access private member declared in class 'MyClass'

错误:C2248:“MyClass: MyClass”:无法访问类“MyClass”中声明的私有成员

I think the second error message is easier to understand for somebody that is new to Qt.

我认为第二个错误信息对于Qt新手来说更容易理解。

The MyClass definition is also self-documenting if Q_DISABLE_COPY is included in the class definition for anybody reading the code.

如果Q_DISABLE_COPY包含在任何阅读代码的人的类定义中,那么MyClass定义也是自文档化的。

Another reason to repeat the definition in derived classes is to protect your code from future bugs if the implementation of QObject is changed to no longer use Q_DISABLE_COPY(). While this is unlikely, by documenting this requirement the Qt developers left themselves a small amount of wiggle room if they ever decide to change QObject.

在派生类中重复定义的另一个原因是,如果将QObject的实现更改为不再使用Q_DISABLE_COPY(),则可以保护代码不受将来的错误影响。虽然这是不可能的,但是如果Qt开发人员决定更改QObject的话,通过记录这一需求,他们会给自己留一些余地。

#1


11  

The error message that might be printed from attempting to copy the derived class might refer to QObject instead of your code, so the error might appear to be confusing. For example, using Visual Studio 2012 to compile this code

试图复制派生类时可能打印的错误消息可能指向QObject而不是您的代码,因此错误可能看起来很混乱。例如,使用Visual Studio 2012来编译这段代码。

class MyClass : public QObject
{

};

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(application);

    MyClass obj1;
    MyClass obj2(obj1);

    QApplication app(argc, argv);
    app.setOrganizationName("QtProject");
    app.setApplicationName("Application Example");
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

results in this error (and a bunch of references to QObject).

导致这个错误(以及对QObject的一系列引用)。

error: C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject'

错误:C2248:“QObject: QObject”:无法访问类“QObject”中声明的私有成员

Changing MyClass to

改变MyClass

class MyClass : public QObject
{
private:
    Q_DISABLE_COPY(MyClass)
public:
    MyClass();
};

results in a much more user friendly group of errors that refer to MyClass, starting with

结果会出现更多的用户友好的错误,这些错误都指向MyClass,首先是。

error: C2248: 'MyClass::MyClass' : cannot access private member declared in class 'MyClass'

错误:C2248:“MyClass: MyClass”:无法访问类“MyClass”中声明的私有成员

I think the second error message is easier to understand for somebody that is new to Qt.

我认为第二个错误信息对于Qt新手来说更容易理解。

The MyClass definition is also self-documenting if Q_DISABLE_COPY is included in the class definition for anybody reading the code.

如果Q_DISABLE_COPY包含在任何阅读代码的人的类定义中,那么MyClass定义也是自文档化的。

Another reason to repeat the definition in derived classes is to protect your code from future bugs if the implementation of QObject is changed to no longer use Q_DISABLE_COPY(). While this is unlikely, by documenting this requirement the Qt developers left themselves a small amount of wiggle room if they ever decide to change QObject.

在派生类中重复定义的另一个原因是,如果将QObject的实现更改为不再使用Q_DISABLE_COPY(),则可以保护代码不受将来的错误影响。虽然这是不可能的,但是如果Qt开发人员决定更改QObject的话,通过记录这一需求,他们会给自己留一些余地。