Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOVLED] Puzzling class member and its usage in class constructor
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 724 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Karen
    wrote on last edited by Karen
    #1

    I inherited a piece of code as the following:

    In the header file:

    namespace Ui
    {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    private:
    Ui::MainWindow *ui;
    ...
    }

    In cpp file, the class constructor as defined as below:
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(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?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      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.

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      K 1 Reply Last reply
      1
      • M mcosta

        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.

        K Offline
        K Offline
        Karen
        wrote on last edited by
        #3

        @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).

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved