具有复制和分配的C ++ Qt反射 [英] C++ Qt Reflection with Copy and Assignment
问题描述
作为 QObject
文档及许多其他解释,QObject
具有一个标识,因此隐藏了其副本构造函数和赋值运算符.
As the QObject
documentation and many others explain, a QObject
has an identity and thus hides its copy constructor and assignment operator.
但是,我不是从QObject
派生而来的,它的动态特性或信号/插槽特性.我只想要反射,或访问Foo::staticMetaObject
的功能.
However, I'm not deriving from QObject
for its dynamic properties feature or the signals/slots feature. I only want reflection, or the ability to access Foo::staticMetaObject
.
class Foo : public QObject {
Q_OBJECT
Q_ENUMS(Color)
public:
enum Color { Blue, Red, Pink };
private:
Color color;
};
Q_DECLARE_METATYPE(Foo::Color)
然后我无法使用以下方式复制Foo
:
I then can't copy Foo
with:
Foo a;
Foo b;
a = b;
在这种情况下,允许复制和分配的最佳方法是什么?我是否绝对需要编写副本构造函数和赋值运算符?他们会是什么样子?反射仍然有效吗?
What's the best way to allow copy and assignment in this case? Do I absolutely need to write a copy constructor and assignment operator? What would they look like? Will reflection still work?
推荐答案
如果您只想对...进行反思
If you are only interested in having reflection for
- 类名
- 枚举和标志(Q_ENUMS,Q_FLAGS)
- 类信息( Q_CLASSINFO ),
- the class name,
- enums and flags (Q_ENUMS, Q_FLAGS),
- class info (Q_CLASSINFO),
您可以使用 Q_GADGET
代替Q_OBJECT
:
you can use Q_GADGET
instead of Q_OBJECT
:
class Foo {
Q_GADGET
Q_ENUMS(Color)
public:
enum Color { Blue, Red, Pink };
private:
Color color;
};
它将声明并定义Foo::staticMetaObject
.
这篇关于具有复制和分配的C ++ Qt反射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!