copy constructor of derived QT class

Karen Tsirunyan

I have a class which is publicly inherited from QWidget:

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    MyWidget(const MyWidget& other)
      :
    obj1(other.obj1),
    obj2(other.obj2)

private:
    some_class obj1;
    some_class obj2;
};

When I built my project, compiler complains:

WARNING:: Base class "class QWidget" should be explicitly initialized in the copy constructor.

I checked out from other questions on stackoverflow, and got my answer. But the fact is, when I added that initialization like this:

class MyWidget : public QWidget
{
    Q_OBJECT
public:
    MyWidget(const MyWidget& other)
      :
    QWidget(other),   //I added the missing initialization of Base class
    obj1(other.obj1),
    obj2(other.obj2)

private:
    some_class obj1;
    some_class obj2;
};

I got compile error:

QWidget::QWidget(const QWidget&) is private within this context

So, please explain me what I am doing wrong.

BЈовић

QObject Class description page tells :

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.

That means you are not supposed to copy QT objects, since QObject is non-copyable by design.

The first warning tells you to initialize the base class (which is QWidget). If you want to do this, you are going to construct a new base object, and I doubt that is what you want to do.

The second error is telling you what I wrote above : do not copy qt objects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Copy constructor with a parameter as a reference to a derived class

From Dev

C++ behavior of a default(implicit) copy constructor in a derived class

From Dev

Making a copy constructor and assignment operator from a derived class

From Dev

Move constructor for derived class

From Dev

Overloaded constructor in derived class

From Dev

Error on constructor in derived class

From Dev

Constructor in base and derived class

From Dev

Assignment operator and copy constructor for class containing base class pointer to derived templated class

From Dev

Can a derived class be made uncopyable by declaring copy constructor/operator private in base class?

From Dev

Base Constructor Call in Derived Class

From Dev

Derived Class Constructor Returning Null

From Dev

Constructor of class derived from QCustomPlot

From Dev

calling a constructor from a derived class

From Dev

Use derived class for template constructor

From Dev

Copy assignment operator for derived class

From Dev

Copy constructor of template class

From Dev

Add copy constructor to a class

From Dev

Visibility of a base class constructor in a derived template class

From Dev

Inadvertent base class constructor "hiding" in derived class

From Dev

Derived Class Constructor from an Abstract Class

From Dev

Use derived class property in base class constructor

From Dev

Calling base class constructor from derived class constructor

From Dev

Calling base class constructor from derived class constructor

From Dev

Fill collection in a proteced constructor of a derived class

From Dev

Undefined reference to in Constructor with QNetworkAccessManager derived class

From Dev

Braces (without constructor) initialization of a derived class

From Dev

C++ CRTP constructor of derived class

From Dev

constexpr for null-initialized constructor in a derived class

From Dev

Calling overloaded base constructor from derived class

Related Related

  1. 1

    Copy constructor with a parameter as a reference to a derived class

  2. 2

    C++ behavior of a default(implicit) copy constructor in a derived class

  3. 3

    Making a copy constructor and assignment operator from a derived class

  4. 4

    Move constructor for derived class

  5. 5

    Overloaded constructor in derived class

  6. 6

    Error on constructor in derived class

  7. 7

    Constructor in base and derived class

  8. 8

    Assignment operator and copy constructor for class containing base class pointer to derived templated class

  9. 9

    Can a derived class be made uncopyable by declaring copy constructor/operator private in base class?

  10. 10

    Base Constructor Call in Derived Class

  11. 11

    Derived Class Constructor Returning Null

  12. 12

    Constructor of class derived from QCustomPlot

  13. 13

    calling a constructor from a derived class

  14. 14

    Use derived class for template constructor

  15. 15

    Copy assignment operator for derived class

  16. 16

    Copy constructor of template class

  17. 17

    Add copy constructor to a class

  18. 18

    Visibility of a base class constructor in a derived template class

  19. 19

    Inadvertent base class constructor "hiding" in derived class

  20. 20

    Derived Class Constructor from an Abstract Class

  21. 21

    Use derived class property in base class constructor

  22. 22

    Calling base class constructor from derived class constructor

  23. 23

    Calling base class constructor from derived class constructor

  24. 24

    Fill collection in a proteced constructor of a derived class

  25. 25

    Undefined reference to in Constructor with QNetworkAccessManager derived class

  26. 26

    Braces (without constructor) initialization of a derived class

  27. 27

    C++ CRTP constructor of derived class

  28. 28

    constexpr for null-initialized constructor in a derived class

  29. 29

    Calling overloaded base constructor from derived class

HotTag

Archive