Blackened Justice
25th February 2012, 00:34
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:
class MyWidget: public QWidget {
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
}
MyWidget::MyWidget(QWidget *parent = 0): QWidget(parent) {
// Code here
}
Is it any different from doing it like this:
class MyWidget: public QWidget {
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
}
MyWidget::MyWidget(QWidget *parent = 0) {
QWidget(parent)
// Code here
}
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
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:
class MyWidget: public QWidget {
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
}
MyWidget::MyWidget(QWidget *parent = 0): QWidget(parent) {
// Code here
}
Is it any different from doing it like this:
class MyWidget: public QWidget {
Q_OBJECT
public:
explicit MyWidget(QWidget *parent = 0);
}
MyWidget::MyWidget(QWidget *parent = 0) {
QWidget(parent)
// Code here
}
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