7

As the QObject documentation and many others explain, a QObject has an identity and thus hides its copy constructor and assignment operator.

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)

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?

2
  • You mean that doesn't using QObject. Because reflection is not the source of the issue here. The issue is that QObject make copy and assignment private (for a good reason). Aug 25, 2011 at 23:54
  • 1
    @kristianp, good suggestion, I would make that edit if @alexisdm didn't provide the exact answer to my exact question. Often times the answer is "don't do what you are asking to do" but in this case, even if that's a reasonable answer, there's also one that does let me do what I want to do: Q_GADGET Aug 30, 2011 at 5:39

3 Answers 3

14
+100

If you are only interested in having reflection for

  • the class name,
  • enums and flags (Q_ENUMS, Q_FLAGS),
  • class info (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.

2
  • Wow, I didn't hear from Q_GADGET before. So it seems to be a lightweight and static-only version of Q_OBJECT which can be very useful in some cases (like this). Sad it's not documented, at least I couldn't find it. This makes it unofficial and thus not guaranteed to work in the future :(
    – leemes
    Oct 14, 2013 at 11:17
  • @leemes It's actually briefly mentioned alongside Q_OBJECT on the QObject page (I added the link above). But you can say it is pretty well hidden, and I only found about it when searching through the moc source code for something else.
    – alexisdm
    Oct 15, 2013 at 23:53
2

You can certainly implement both a copy constructor and a copy assignment operator in your derived class, but it's likely a sign of bad 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;
}

This will compile just fine. However, as expected, when you run the resulting program, all that is printed is Derived copy constructor invoked!. When the base class declares its copy constructor/copy assignment operator as private, that doesn't prevent derived classes from implementing their own versions. It simply prevents derived classes from calling the base class versions.

And therein lies the problem: it's always good practice to make sure you copy all parts of an object, so that you indeed have two distinct copies. Part of your object includes the data owned by the base class, so you should always make sure to invoke the base class's copy constructor/copy assignment operator to ensure that a full copy is made. But that data is by design non-copyable. Thus, it is impossible to copy all parts of the object.

It's up to you if you want to stick with this design. One important thing to ask yourself is, does your derived class really need to be copyable at all? If not, then there's nothing to worry about!

1

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

Still if you insist then memcpy can be your last resort. I don't recommend it personally, because you have to take care about deep copying, vtable etc. which are not always trivial.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.