C ++ Qt Reflection with Copy and Assignment

As the documentation of QObject and many others explain, QObject has an identifier and thus hides its constructor and assignment operator.

However, I do not get a dynamic property function or a signal / slot function from QObject . I only need 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) 

Then I can not copy Foo with:

 Foo a; Foo b; a = b; 

What is the best way to allow copying and assignment in this case? Do I need to write a constructor operator and an assignment operator? How do they look? Will it reflect the work?

+6
source share
3 answers

If you are only interested in reflection for

  • class name
  • listings and flags (Q_ENUMS, Q_FLAGS),
  • class information ( Q_CLASSINFO ),

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; }; 

which will declare and define Foo::staticMetaObject .

+14
source

You can, of course, implement both the copy constructor and the copy assignment operator in a derived class, but this is probably a sign of poor design. Take this example:

 #include <iostream> class Base { public: Base() {} private: Base(const Base& other) { std::cout << "Base copy constructor invoked!" << std::endl; } }; class Derived : public Base { public: Derived() {} Derived(const Derived& other) { std::cout << "Derived copy constructor invoked!" << std::endl; } }; int main(int argc, char** argv) { Derived a; Derived b = a; return 0; } 

It will be just fine. However, as expected, when you run the resulting program, all the printed value Derived copy constructor invoked! . When a base class declares its copy / copy constructor statement as private, this does not prevent derived classes from implementing their own versions. It simply prohibits derived classes from invoking base class versions .

And therein lies the problem: it is always good practice to make sure that you copy all parts of the object, so that you really have two different copies. Part of your object includes data that belongs to the base class, so you should always be sure that you invoke the constructor statement of the base class instance / copy instance to ensure a full copy. But this design data is not copied. Thus, it is not possible to copy all parts of an object.

This is for you if you want to stick to this design. One important thing to ask yourself is, does your derived class really need to be copied at all? If not, then there is nothing to worry about!

+2
source

I don't know much about qt , but if the copy constructor is not allowed, then there must be a reason for this (as mentioned in the link you provided). You can change your design to not have it.

However, if you insist, memcpy may be your last resort. I do not recommend it personally, because you have to take care of deep copying, vtable, etc., which are not always trivial.

+1
source

Source: https://habr.com/ru/post/893396/


All Articles