MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
In this secti
These two lines are the so-called initialization list and are executed at "creation" time of each instance of this class. Each class inheriting another should contain a call to the superclass' constructor in this list.
You could also write:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
ui = new Ui::MainWindow();
ui->setupUi(this);
}
which one could find better readable. But using an initialization list is slightly faster and is being optimized by the compiler. Note that these lists can only be used in constructors and you cannot call any functions of the object - because it doesn't "live" yet. But you may set the value of some attributes and refer to them in following statements (to avoid code redundancy for example), like in the following example:
#define DEFAULT_VALUE 1.0
class MyClass {
public:
MyClass() :
value1(DEFAULT_VALUE),
value2(value1)
{
}
MyClass(qreal value) :
value1(value),
value2(value1)
{
}
private:
qreal value1;
qreal value2;
};
Note that most compilers give you a warning if the order of the members in your initialization list doesn't match the order in the class definition.