1

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.

Community
  • 1
  • 1
user1284631
  • 4,446
  • 36
  • 61
  • Yes, you are right. almost the same, except that in that question, the definition and the declarationa re combined. For a newbie like me in C++ (I only know C...), seeing that succession of "::" and ":" lost me. Thank you for pointing out the other question. – user1284631 May 03 '12 at 09:42
  • 1
    No Problem :), Just so you know this construct is called as an **Member Initialization List** in C++. – Alok Save May 03 '12 at 09:43

1 Answers1

2

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Thank you very much for the quick answer. Is there no similar super() in C++ for directly calling the base class constructor inside the derived class constructor? – user1284631 May 03 '12 at 09:36
  • 2
    @axeoth **this is** the equivalent of calling `super()`. No way to call a base constructor on the same object in C++ other than this, no. – Luchian Grigore May 03 '12 at 09:37
  • Thank you very much. Indeed, I found the answers confirmed here: http://www.comp.lancs.ac.uk/~marash/SlidesCpp/slides/tsld081.htm and here: http://www.horstmann.com/ccj2/ccjapp3.html. I just did not know what to search for. You were very kind to answer. – user1284631 May 03 '12 at 09:39
  • From this example: http://www.comp.lancs.ac.uk/~marash/SlidesCpp/slides/tsld081.htm I understand that I could have done inside my constructor something like: this->QMainWindow::QMainWindow(parent); that is, just like calling super(). Can anybody confirm that is correct? – user1284631 May 03 '12 at 10:11
  • yep, apparently, the this->QMainWindow::QMainWindow(parent); does not work, the error is: "error: cannot call constructor 'QMainWindow::QMainWindow' directly" – user1284631 May 03 '12 at 11:08