Question

Possible Duplicate:
What is this weird colon-member syntax in the constructor?

I am in the need of a quick answer to the following question. It is about a C++ class (a QT-derivet class, but I think that is not important here).

In the header file, I have the following declaration:

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

while in the source file I have the following definition:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    QWidget * central = new QWidget(this);

The question, maybe showing clearly my noobiness when it comes to C++, it is:

what does mean that : QMainWindow(parent) in the definition of the constructor? Is some kind of default initialization? Is that linked to the fact that the constructor is explicit (I have som grasp on what that does mean, but no detailed one)?

Please, I know the question might be very simple and noob, I just do not know where to start.

Thank you.

edit: Thank you to everybody that answered.

Was it helpful?

Solution

It calls that specific constructor from the base class.

It has nothing to do with the constructor being explicit. Explicit means, in this case, that a QWidget* cannot be implicitly converted to a MainWindow object.

The :QMainWindow(parent) simply says that the base class constructor which takes a QWidget* as parameter should be called to construct the object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top