Solved Why we write QWidget *parent=0?
-
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QDebug> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_ok_button_clicked(); void on_cancel_button_clicked(); private: Ui::MainWindow *ui; };
What is meant by explicit MainWindow(QWidget *parent=0)?
why we write Qwidget *parent=0?
Is itokay if we dont write explicit? -
-
When you write:
explicit MainWindow(QWidget *parent);
you will have to assign a parent, because it expects an argument.
But when you write:explicit MainWindow(QWidget *parent = 0);
you don't have to assign a parent, as it has already a default argument.
As for the second question, perhaps this gives you some insight?
https://msdn.microsoft.com/en-us/library/wwywka61.aspx -
As a side note, nullptr is a better replacement for 0.
-
@tham ... I agree that the use of '0' is not good for pointers as it is ambiguous. But, for < c++11 nullptr is not defined. That being said, Q_NULLPTR could be used and that would be, at least, more readable as to intent.