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. Explain me how this constructor works!

Explain me how this constructor works!

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 1.2k 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.
  • Nio74N Offline
    Nio74N Offline
    Nio74
    wrote on last edited by
    #1

    this is the constructor:

    ClientServices::ClientServices(const QString hostAddress, int portNumber) : QObject (),m_nNextBlockSize(0)
    {
    

    I would like to understand after colon.(m_nNextBlockSize(0) it's declared on the header file like this "quint16 m_nNextBlockSize;" why?

    can you give me some tips about the type of constructors? Not having much experience I need help i don't understend them very well..

    ODБOïO 1 Reply Last reply
    0
    • Nio74N Nio74

      this is the constructor:

      ClientServices::ClientServices(const QString hostAddress, int portNumber) : QObject (),m_nNextBlockSize(0)
      {
      

      I would like to understand after colon.(m_nNextBlockSize(0) it's declared on the header file like this "quint16 m_nNextBlockSize;" why?

      can you give me some tips about the type of constructors? Not having much experience I need help i don't understend them very well..

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @Nio74 hi, the constructor will initialize that quint16 m_nNextBlockSize declared in the header

      Nio74N 1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        After the ":", its actually an initialization list
        https://en.cppreference.com/w/cpp/language/initializer_list

        basically its another syntax for
        m_nNextBlockSize =0;

        so its a way to set member variables during construction.

        1 Reply Last reply
        5
        • ODБOïO ODБOï

          @Nio74 hi, the constructor will initialize that quint16 m_nNextBlockSize declared in the header

          Nio74N Offline
          Nio74N Offline
          Nio74
          wrote on last edited by
          #4

          @LeLev said in Explain me how this constructor works!:

          @Nio74 hi, the constructor will initialize that quint16 m_nNextBlockSize declared in the header

          why do you decare(quint16 m_nNextBlockSize ) in the constructor when it's already declared in the header file?

          mrjjM 1 Reply Last reply
          0
          • Nio74N Nio74

            @LeLev said in Explain me how this constructor works!:

            @Nio74 hi, the constructor will initialize that quint16 m_nNextBlockSize declared in the header

            why do you decare(quint16 m_nNextBlockSize ) in the constructor when it's already declared in the header file?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Nio74
            hi
            it doesn't declare there. ( there is no type, just the name)
            it just sets its value.

            Nio74N 1 Reply Last reply
            5
            • mrjjM mrjj

              @Nio74
              hi
              it doesn't declare there. ( there is no type, just the name)
              it just sets its value.

              Nio74N Offline
              Nio74N Offline
              Nio74
              wrote on last edited by Nio74
              #6

              @mrjj but then when it is declared in the header file it has no value, and instead by calling it in the constructor it is given a value which in this case is 0 , right?

              hskoglundH 1 Reply Last reply
              0
              • Nio74N Nio74

                @mrjj but then when it is declared in the header file it has no value, and instead by calling it in the constructor it is given a value which in this case is 0 , right?

                hskoglundH Offline
                hskoglundH Offline
                hskoglund
                wrote on last edited by
                #7

                @Nio74 Yes, but since this example is for Qt 4.8 and kind of old, starting with C++11 you can also kill two birds with one stone by just declaring the that value directly in the header file, like:

                quint16 m_nNextBlockSize = 0; 
                
                1 Reply Last reply
                4
                • Nio74N Offline
                  Nio74N Offline
                  Nio74
                  wrote on last edited by
                  #8

                  Tanks to everyone for help me.

                  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