我有一个是公开从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;
};
当我建我的项目,编译器抱怨:
WARNING:: Base class "class QWidget" should be explicitly initialized in the copy constructor.
我从其他的问题检查出来stackoverflow,并得到了我的答案。 但事实是,当我添加了初始化像这样:
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;
};
我得到了编译错误:
QWidget::QWidget(const QWidget&) is private within this context
所以,请给我解释一下我在做什么错。
似乎'QWidget'没有设计成拷贝构造,这意味着你的派生类型不应该是。 – juanchopanza
你是否明确地为'QWidget'创建了一个拷贝构造函数,或者你把它留给了编译器? – Olayinka
我不需要为QWidget创建一个Copy-Constructor。我可以在我的对象的拷贝构造函数的init列表中初始化调用QWidget :: Cpoy-Constructor的对象。 –