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. Constructor syntax in Qt in mainwindow.cpp
Forum Updated to NodeBB v4.3 + New Features

Constructor syntax in Qt in mainwindow.cpp

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 5 Posters 2.4k Views 1 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.
  • S Offline
    S Offline
    Swati777999
    wrote on 25 Nov 2021, 01:50 last edited by
    #1

    Hi All,

    I have a doubt about the syntax of the constructor implemented in the mainwindow.cpp file. Below is an example of a constructor defined in mainwindow.cpp file. I would like to be confirmed about this part of the syntax . I feel that it shows the inheritance as MainWindow is inherited from QMainWindow.

    Doubts:

    1. Why is the argument i.e parent same for both MainWindow and QMainWindow , for eg. MainWindow(QWidget *parent)
      : QMainWindow(parent) . For MainWindow , the argument parent is of type QWidget , does this parent has same type with QMainWindow?

    2. What are name(0), place(0),thing(0), about(0) in the definition ?
      : QMainWindow(parent),
      name(0),
      place(0),
      thing(0),
      about(0)

       MainWindow::MainWindow(QWidget *parent)
       : QMainWindow(parent),
       name(0),
       place(0),
       thing(0),
       about(0)
      

    “ In order to be irreplaceable, one must always be different” – Coco Chanel

    J 1 Reply Last reply 25 Nov 2021, 05:25
    0
    • S Swati777999
      25 Nov 2021, 09:29

      @JonB Your syntax is what I had expected and seen many times before. This is how the variables are initialized.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 25 Nov 2021, 09:33 last edited by
      #11

      @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

      This is how the variables are initialized.

      This is a possibility. But in proper C++ projects this is not the way members are initialised. Such code would not get approved in projects I work on unless there is really a need to do it this way.
      Correct way to initialise members is using the syntax you were asking about. So, I suggest you learn and use it. This is also nothing new, exists in C++ for ages already.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Swati777999
        25 Nov 2021, 01:50

        Hi All,

        I have a doubt about the syntax of the constructor implemented in the mainwindow.cpp file. Below is an example of a constructor defined in mainwindow.cpp file. I would like to be confirmed about this part of the syntax . I feel that it shows the inheritance as MainWindow is inherited from QMainWindow.

        Doubts:

        1. Why is the argument i.e parent same for both MainWindow and QMainWindow , for eg. MainWindow(QWidget *parent)
          : QMainWindow(parent) . For MainWindow , the argument parent is of type QWidget , does this parent has same type with QMainWindow?

        2. What are name(0), place(0),thing(0), about(0) in the definition ?
          : QMainWindow(parent),
          name(0),
          place(0),
          thing(0),
          about(0)

           MainWindow::MainWindow(QWidget *parent)
           : QMainWindow(parent),
           name(0),
           place(0),
           thing(0),
           about(0)
          
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 25 Nov 2021, 05:25 last edited by jsulm
        #2

        @Swati777999 Please read https://doc.qt.io/qt-5/objecttrees.html
        And also learn about C++ constructors.

        1. Parent is of type QWidget* because any widget can be parent of other widgets. "does this parent has same type with QMainWindow?" - why don't you check documentation (https://doc.qt.io/qt-5/qmainwindow.html#QMainWindow)?
        2. This has nothing to do with Qt. name, place, thing and about seem to be member variables in your MainWindow class which are initialised this way in constructor. This are C++ basics, has nothing to do with Qt.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        S 1 Reply Last reply 25 Nov 2021, 06:50
        2
        • J jsulm
          25 Nov 2021, 05:25

          @Swati777999 Please read https://doc.qt.io/qt-5/objecttrees.html
          And also learn about C++ constructors.

          1. Parent is of type QWidget* because any widget can be parent of other widgets. "does this parent has same type with QMainWindow?" - why don't you check documentation (https://doc.qt.io/qt-5/qmainwindow.html#QMainWindow)?
          2. This has nothing to do with Qt. name, place, thing and about seem to be member variables in your MainWindow class which are initialised this way in constructor. This are C++ basics, has nothing to do with Qt.
          S Offline
          S Offline
          Swati777999
          wrote on 25 Nov 2021, 06:50 last edited by
          #3

          @jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.

          :QMainWindow(parent),
          name(0),
          place(0),
          thing(0),
          about(0)
          

          Generally, in C++ , any function is declared as
          void custom_func (arg1,arg2......argn)
          {
          ............
          }

          Please clarify the above syntax of Qt constructor declaration.

          “ In order to be irreplaceable, one must always be different” – Coco Chanel

          J J K 3 Replies Last reply 25 Nov 2021, 06:54
          0
          • S Swati777999
            25 Nov 2021, 06:50

            @jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.

            :QMainWindow(parent),
            name(0),
            place(0),
            thing(0),
            about(0)
            

            Generally, in C++ , any function is declared as
            void custom_func (arg1,arg2......argn)
            {
            ............
            }

            Please clarify the above syntax of Qt constructor declaration.

            J Offline
            J Offline
            jsulm
            Lifetime Qt Champion
            wrote on 25 Nov 2021, 06:54 last edited by
            #4

            @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

            I don't understand how the member variables are represented in the declaration of the constructor with commas.

            Please learn C++! You are asking absolute basics!

            class SomeClass
            {
            public:
                SomeClass():
                    a(1),
                    b("Some String")
                {}
            private:
                int a;
                std::string b;
            };
            

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply 25 Nov 2021, 09:02
            3
            • S Swati777999
              25 Nov 2021, 06:50

              @jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.

              :QMainWindow(parent),
              name(0),
              place(0),
              thing(0),
              about(0)
              

              Generally, in C++ , any function is declared as
              void custom_func (arg1,arg2......argn)
              {
              ............
              }

              Please clarify the above syntax of Qt constructor declaration.

              J Offline
              J Offline
              JonB
              wrote on 25 Nov 2021, 08:22 last edited by JonB
              #5

              @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

              Generally, in C++ , any function is declared as
              void custom_func (arg1,arg2......argn)
              {

              This is a plain C function declaration. The example you are asking about is for a C++ class method, and inheriting from a base class.

              It does not declare the variables in the comma-list, it merely initializes them. They must be declared as class members, as @jsulm has shown. In this particular case it is just equivalent to:

               MainWindow::MainWindow(QWidget *parent)
               : QMainWindow(parent)
              {
                   name = 0;
                   place = 0;
                   thing = 0;
                   about = 0;
                  ...
              }
              
              S 1 Reply Last reply 25 Nov 2021, 09:29
              2
              • S Swati777999
                25 Nov 2021, 06:50

                @jsulm I don't understand how the member variables are represented in the declaration of the constructor with commas.

                :QMainWindow(parent),
                name(0),
                place(0),
                thing(0),
                about(0)
                

                Generally, in C++ , any function is declared as
                void custom_func (arg1,arg2......argn)
                {
                ............
                }

                Please clarify the above syntax of Qt constructor declaration.

                K Offline
                K Offline
                KroMignon
                wrote on 25 Nov 2021, 08:34 last edited by
                #6

                @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                I don't understand how the member variables are represented in the declaration of the constructor with commas.

                As told by @jsulm : this is C++ basic knowledge: https://en.cppreference.com/w/cpp/language/constructor

                As Qt is a C++ framework, it will really help you, and save from frustrations, to to first learn C++ basics.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                1 Reply Last reply
                0
                • J jsulm
                  25 Nov 2021, 06:54

                  @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                  I don't understand how the member variables are represented in the declaration of the constructor with commas.

                  Please learn C++! You are asking absolute basics!

                  class SomeClass
                  {
                  public:
                      SomeClass():
                          a(1),
                          b("Some String")
                      {}
                  private:
                      int a;
                      std::string b;
                  };
                  
                  S Offline
                  S Offline
                  Swati777999
                  wrote on 25 Nov 2021, 09:02 last edited by
                  #7

                  @jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.

                  I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.

                  “ In order to be irreplaceable, one must always be different” – Coco Chanel

                  K J 2 Replies Last reply 25 Nov 2021, 09:12
                  0
                  • S Swati777999
                    25 Nov 2021, 09:02

                    @jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.

                    I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 25 Nov 2021, 09:12 last edited by
                    #8

                    @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                    have never found the syntax of C++ function like this before

                    Because this is not a function but a class constructor declaration.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    S 1 Reply Last reply 25 Nov 2021, 09:42
                    0
                    • S Swati777999
                      25 Nov 2021, 09:02

                      @jsulm I'm familiar with the private variables declaration in your code not with the function declared in the public section.

                      I've taken up courses of C++ in one of my undergraduate curriculum but have never found the syntax of C++ function like this before. So, put this question.

                      J Offline
                      J Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on 25 Nov 2021, 09:13 last edited by
                      #9

                      @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                      syntax of C++ function like this before

                      This syntax is only valid for C++ constructors, not normal functions/methods.
                      A C++ course should actually cover this basic thing.
                      Please follow the link provided by @KroMignon

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • J JonB
                        25 Nov 2021, 08:22

                        @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                        Generally, in C++ , any function is declared as
                        void custom_func (arg1,arg2......argn)
                        {

                        This is a plain C function declaration. The example you are asking about is for a C++ class method, and inheriting from a base class.

                        It does not declare the variables in the comma-list, it merely initializes them. They must be declared as class members, as @jsulm has shown. In this particular case it is just equivalent to:

                         MainWindow::MainWindow(QWidget *parent)
                         : QMainWindow(parent)
                        {
                             name = 0;
                             place = 0;
                             thing = 0;
                             about = 0;
                            ...
                        }
                        
                        S Offline
                        S Offline
                        Swati777999
                        wrote on 25 Nov 2021, 09:29 last edited by
                        #10

                        @JonB Your syntax is what I had expected and seen many times before. This is how the variables are initialized.

                        “ In order to be irreplaceable, one must always be different” – Coco Chanel

                        J 1 Reply Last reply 25 Nov 2021, 09:33
                        0
                        • S Swati777999
                          25 Nov 2021, 09:29

                          @JonB Your syntax is what I had expected and seen many times before. This is how the variables are initialized.

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 25 Nov 2021, 09:33 last edited by
                          #11

                          @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                          This is how the variables are initialized.

                          This is a possibility. But in proper C++ projects this is not the way members are initialised. Such code would not get approved in projects I work on unless there is really a need to do it this way.
                          Correct way to initialise members is using the syntax you were asking about. So, I suggest you learn and use it. This is also nothing new, exists in C++ for ages already.

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          2
                          • K KroMignon
                            25 Nov 2021, 09:12

                            @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                            have never found the syntax of C++ function like this before

                            Because this is not a function but a class constructor declaration.

                            S Offline
                            S Offline
                            Swati777999
                            wrote on 25 Nov 2021, 09:42 last edited by
                            #12

                            @KroMignon Precisely, a constructor is a member function of a class.

                            “ In order to be irreplaceable, one must always be different” – Coco Chanel

                            J K 2 Replies Last reply 25 Nov 2021, 09:44
                            0
                            • S Swati777999
                              25 Nov 2021, 09:42

                              @KroMignon Precisely, a constructor is a member function of a class.

                              J Offline
                              J Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on 25 Nov 2021, 09:44 last edited by
                              #13

                              @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                              a constructor is a member function of a class

                              It is a special member function of a class

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              J 1 Reply Last reply 25 Nov 2021, 09:55
                              0
                              • S Swati777999
                                25 Nov 2021, 09:42

                                @KroMignon Precisely, a constructor is a member function of a class.

                                K Offline
                                K Offline
                                KroMignon
                                wrote on 25 Nov 2021, 09:46 last edited by
                                #14

                                @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                                Precisely, a constructor is a member function of a class.

                                I don't understand what your goal is?
                                You ask a question about very basic C++ syntax, for which you've got answer and pointer to C++ documentation to get more detailed explanation, but you don't seems to accept those responses.
                                Why?
                                Do I hurt you in any way?

                                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                1 Reply Last reply
                                0
                                • J jsulm
                                  25 Nov 2021, 09:44

                                  @Swati777999 said in Constructor syntax in Qt in mainwindow.cpp:

                                  a constructor is a member function of a class

                                  It is a special member function of a class

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 25 Nov 2021, 09:55 last edited by
                                  #15

                                  @jsulm Here an explanation why this specual syntax is better:

                                  class A
                                  {
                                  public:
                                      A() { std::cout << "A" << std::endl; }
                                      A(int) { std::cout << "A(int)" << std::endl; }
                                  };
                                  
                                  class B
                                  {
                                  public:
                                      B() { a = A(1); }
                                  
                                  private:
                                      A a;
                                  };
                                  
                                  B b;
                                  

                                  In the above code you will see that both A constructors are called, even though you are creating only one A instance explicetly. Reason is that members are initialised for you if you don't do it explicetly. In this case a is initialised using default constructor first, but then you create a new one using the other constructor and assign it to a.
                                  If you change

                                  B() { a = A(1); }
                                  

                                  to

                                  B()
                                      : a(1)
                                  {  }
                                  

                                  you will see that only one constructor is called. So, it is more efficient to use the special constructor syntax.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  M 1 Reply Last reply 25 Nov 2021, 12:25
                                  3
                                  • J jsulm
                                    25 Nov 2021, 09:55

                                    @jsulm Here an explanation why this specual syntax is better:

                                    class A
                                    {
                                    public:
                                        A() { std::cout << "A" << std::endl; }
                                        A(int) { std::cout << "A(int)" << std::endl; }
                                    };
                                    
                                    class B
                                    {
                                    public:
                                        B() { a = A(1); }
                                    
                                    private:
                                        A a;
                                    };
                                    
                                    B b;
                                    

                                    In the above code you will see that both A constructors are called, even though you are creating only one A instance explicetly. Reason is that members are initialised for you if you don't do it explicetly. In this case a is initialised using default constructor first, but then you create a new one using the other constructor and assign it to a.
                                    If you change

                                    B() { a = A(1); }
                                    

                                    to

                                    B()
                                        : a(1)
                                    {  }
                                    

                                    you will see that only one constructor is called. So, it is more efficient to use the special constructor syntax.

                                    M Offline
                                    M Offline
                                    mpergand
                                    wrote on 25 Nov 2021, 12:25 last edited by mpergand
                                    #16

                                    To go along with @jsulm
                                    With const and reference variables, using the initializer list is mandatory.

                                    class B
                                    {
                                    public:
                                        B() 
                                            { 
                                            a = A(1);
                                            str="hello";  // error
                                            }
                                    
                                    private:
                                        A a;
                                        const string str;
                                    };
                                    

                                    Funny enough, that way you can initialize a const var two times !

                                    class B
                                    {
                                    public:
                                        B() : str("hello2") 
                                            { 
                                            a = A(1);
                                    
                                            }
                                    
                                    private:
                                        A a;
                                        const string str="hello";  // since  c++11
                                    };
                                    
                                    1 Reply Last reply
                                    3

                                    1/16

                                    25 Nov 2021, 01:50

                                    • Login

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