Hey everyone,
I have a (rather simple) question about C++ and constructors/inheritance. Usually, when reading Qt tutorials, I see something like this for the definition of the class's constructor:
Q_OBJECT
public:
explicit MyWidget
(QWidget *parent
= 0);
}
// Code here
}
class MyWidget: public QWidget {
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
}
MyWidget::MyWidget(QWidget *parent = 0): QWidget(parent) {
// Code here
}
To copy to clipboard, switch view to plain text mode
Is it any different from doing it like this:
Q_OBJECT
public:
explicit MyWidget
(QWidget *parent
= 0);
}
MyWidget
::MyWidget(QWidget *parent
= 0) { // Code here
}
class MyWidget: public QWidget {
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
}
MyWidget::MyWidget(QWidget *parent = 0) {
QWidget(parent)
// Code here
}
To copy to clipboard, switch view to plain text mode
My guess is that QWidget(parent) is a call to the superclass's constructor, leaving the MyWidget constructor to initialize only what else it adds on to it. And maybe that's why it's put outside the body of the function. Put aren't they in practice the same?
Cheers
Bookmarks