gpt4 book ai didi

c++ - 使用带有 Q_DECLARE_METATYPE 的默认生成的复制构造函数

转载 作者:行者123 更新时间:2023-11-30 05:30:15 25 4
gpt4 key购买 nike

假设我有这个自定义类

struct Analytics : public QObject
{
Q_OBJECT

public:
int a = 12;

Analytics(const Analytics& a) {
}

Analytics(){
}

};

Q_DECLARE_METATYPE(Analytics)

以上工作正常。但是我必须提供自己的复制构造函数。我尝试使用默认生成的复制构造函数进行此操作,但出现错误

结构分析:公共(public) QObject { Q_OBJECT

  public:
int a = 12;

Analytics(const Analytics& a) = default;

Analytics(){
}

};

Q_DECLARE_METATYPE(Analytics)

这是我得到的错误

  5: error: call to implicitly-deleted copy constructor of 'Analytics'
return new (where) T(*static_cast<const T*>(t));

是否可以将 Q_DECLARE_METATYPE 与默认生成的复制构造函数一起使用。

最佳答案

来自文档:

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.

所以你不能复制QObjects和派生自QObject的类。如果您想对 QObjects 使用信号和槽机制,请改用指针。

关于c++ - 使用带有 Q_DECLARE_METATYPE 的默认生成的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36047711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com