从定义的复制构造函数调用默认(隐式)复制构造函数
calling default (implicit) copy constructor from the defined copy constructor
我已经阅读了很多关于这个的话题,但我找不到这个问题的答案
在我的 Qt 应用程序中,我使用 QSignalSpy to catch a signal. It has a user-defined datatype for one of its parameters. To catch such a parameter, I have to first register that datatype with Qt using QMetaType and using the macro Q_DECLARE_METATYPE。它说
This macro makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the type Type as a custom type in QVariant.
问题:我有一个 class CustomData 只定义了构造函数。现在,除非我显式声明析构函数和复制构造函数,否则 Qt 会抛出错误。我想使用 C++ 提供的隐式析构函数和复制构造函数。对于析构函数,我使用了
~CustomData() = default;
它使用默认的析构函数。但是我不能对复制构造函数使用类似的语句。将使用
CustomData( const CustomData& ) {};
调用隐式拷贝构造函数?
(我这样做是因为我想保留隐式复制构造函数的行为)
提前致谢。
CustomData class 如下所示
#include <QMetaType>
#include <QString>
class CustomData : public QObject
{
Q_OBJECT
public:
CustomData(QObject *parent = NULL);
~CustomData() = default; // I added this line
//Will the next line call the implicit copy constructor?
CustomData(const CustomData&) {}; //I added this line
enum CustomMode {mode1, mode2, mode3};
void somePublicMethod();
signals:
void completed(CustomData *data);
private slots:
void customComplete();
private:
CustomMode _mode;
QString _path;
CustomData *_chained;
};
Q_DECLARE_METATYPE(CustomData)
简而言之——没关系。您的实施不会(希望)造成任何影响。
如果你通过Qt documentation,它表示如下
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.
完成这个 -- Q_DISABLE_COPY().
这要求您使用指针来完成工作。看看这个 thread,它讨论了如何在 Qt 中复制对象。
实际上,给定的代码在 Visual Studio 2013 年编译时没有错误。但是,如果信号的签名更改为 void completed(CustomData data);
,则会发生类似的错误。由于您没有指定编译器,但我假设它是 gcc/clang 您可能会收到错误,因为模板处理略有不同。如果是这样,错误很可能是由 Q_DECLARE_METATYPE(CustomData)
引起的,因为它试图在内部生成复制构造函数。将其更改为 Q_DECLARE_METATYPE(CustomData*)
(因为这是您真正需要的,您正在排队 CustomData*
而不是 CustomData
类型的参数,至少在给定的示例中是这样)并且如果您真的没有信号按值发送 CustomData
个实例应该没问题。
此外,如果您通过指针传递对象,那么您应该使用 Q_DECLARE_METATYPE(CustomData*)
。但我建议通过 std::unique_ptr
或 std::shared_ptr
传递它以防止内存泄漏。
我已经阅读了很多关于这个的话题,但我找不到这个问题的答案
在我的 Qt 应用程序中,我使用 QSignalSpy to catch a signal. It has a user-defined datatype for one of its parameters. To catch such a parameter, I have to first register that datatype with Qt using QMetaType and using the macro Q_DECLARE_METATYPE。它说
This macro makes the type Type known to QMetaType as long as it provides a public default constructor, a public copy constructor and a public destructor. It is needed to use the type Type as a custom type in QVariant.
问题:我有一个 class CustomData 只定义了构造函数。现在,除非我显式声明析构函数和复制构造函数,否则 Qt 会抛出错误。我想使用 C++ 提供的隐式析构函数和复制构造函数。对于析构函数,我使用了
~CustomData() = default;
它使用默认的析构函数。但是我不能对复制构造函数使用类似的语句。将使用
CustomData( const CustomData& ) {};
调用隐式拷贝构造函数?
(我这样做是因为我想保留隐式复制构造函数的行为) 提前致谢。
CustomData class 如下所示
#include <QMetaType>
#include <QString>
class CustomData : public QObject
{
Q_OBJECT
public:
CustomData(QObject *parent = NULL);
~CustomData() = default; // I added this line
//Will the next line call the implicit copy constructor?
CustomData(const CustomData&) {}; //I added this line
enum CustomMode {mode1, mode2, mode3};
void somePublicMethod();
signals:
void completed(CustomData *data);
private slots:
void customComplete();
private:
CustomMode _mode;
QString _path;
CustomData *_chained;
};
Q_DECLARE_METATYPE(CustomData)
简而言之——没关系。您的实施不会(希望)造成任何影响。
如果你通过Qt documentation,它表示如下
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.
完成这个 -- Q_DISABLE_COPY().
这要求您使用指针来完成工作。看看这个 thread,它讨论了如何在 Qt 中复制对象。
实际上,给定的代码在 Visual Studio 2013 年编译时没有错误。但是,如果信号的签名更改为 void completed(CustomData data);
,则会发生类似的错误。由于您没有指定编译器,但我假设它是 gcc/clang 您可能会收到错误,因为模板处理略有不同。如果是这样,错误很可能是由 Q_DECLARE_METATYPE(CustomData)
引起的,因为它试图在内部生成复制构造函数。将其更改为 Q_DECLARE_METATYPE(CustomData*)
(因为这是您真正需要的,您正在排队 CustomData*
而不是 CustomData
类型的参数,至少在给定的示例中是这样)并且如果您真的没有信号按值发送 CustomData
个实例应该没问题。
此外,如果您通过指针传递对象,那么您应该使用 Q_DECLARE_METATYPE(CustomData*)
。但我建议通过 std::unique_ptr
或 std::shared_ptr
传递它以防止内存泄漏。