Important: Please read the Qt Code of Conduct -

[SOVLED] Puzzling class member and its usage in class constructor

  • I inherited a piece of code as the following:

    In the header file:

    namespace Ui
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    Ui::MainWindow *ui;

    In cpp file, the class constructor as defined as below:
    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    Question is why a pointer to itself in the header file? Is this not just "this" as in C++?

    Then in the constructor, ui(new Ui::MainWindow) is used in the constructor. What does it do to the constructor?

  • Hi and welcome to devnet,

    'ui' is not an instance of the MainWindow class but of the Ui::MainWindow one that is defined inside the namespace Ui.

  • @mcosta Thank you. Indeed I found the class definition for Ui::MainWindow. So the constructor initializes the member class *ui using ui(new Ui::MainWindow).

Log in to reply