Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. member initialization in definition and c'tor: redundant?
Forum Updated to NodeBB v4.3 + New Features

member initialization in definition and c'tor: redundant?

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 468 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 13 Nov 2023, 18:33 last edited by mzimmers
    #1

    Hi all -

    Consider the following struct (could just as easily be a class):

    struct Pump: Equipment {       
        QString m_fwVersion = QStringLiteral("");
        QString m_address = QStringLiteral("");
        ApplicationType m_application = INVALID_APPLICATION;
        PumpCategory m_pumpCategory = INVALID_CATEGORY;
        int m_primeDuration = INVALID_DURATION;
        int m_primeSpeed = INVALID_SPEED;
        int m_minSpeed = INVALID_SPEED;
        int m_maxSpeed = INVALID_SPEED;
        int m_freezeSpeed = INVALID_SPEED;
        
    public:
        Pump(
            QString fwVersion = QStringLiteral(""),
            QString address = QStringLiteral(""),
            ApplicationType application = INVALID_APPLICATION,
            PumpCategory pumpCategory = INVALID_CATEGORY,
            int primeDuration = INVALID_DURATION,
            int primeSpeed = INVALID_SPEED,
            int minSpeed = INVALID_SPEED,
            int maxSpeed = INVALID_SPEED,
            int freezeSpeed = INVALID_SPEED
            );
    };
    

    I'm wondering whether it's redundant to initialize the members both in-line, and in the c'tor. It would seem so, as the c'tor will do the initialization whenever a new object is constructed. But, it seems kind of negligent not to initialize all the members inline.

    What do the experts do?

    Thanks...

    J 1 Reply Last reply 13 Nov 2023, 18:52
    0
    • M mzimmers
      13 Nov 2023, 18:33

      Hi all -

      Consider the following struct (could just as easily be a class):

      struct Pump: Equipment {       
          QString m_fwVersion = QStringLiteral("");
          QString m_address = QStringLiteral("");
          ApplicationType m_application = INVALID_APPLICATION;
          PumpCategory m_pumpCategory = INVALID_CATEGORY;
          int m_primeDuration = INVALID_DURATION;
          int m_primeSpeed = INVALID_SPEED;
          int m_minSpeed = INVALID_SPEED;
          int m_maxSpeed = INVALID_SPEED;
          int m_freezeSpeed = INVALID_SPEED;
          
      public:
          Pump(
              QString fwVersion = QStringLiteral(""),
              QString address = QStringLiteral(""),
              ApplicationType application = INVALID_APPLICATION,
              PumpCategory pumpCategory = INVALID_CATEGORY,
              int primeDuration = INVALID_DURATION,
              int primeSpeed = INVALID_SPEED,
              int minSpeed = INVALID_SPEED,
              int maxSpeed = INVALID_SPEED,
              int freezeSpeed = INVALID_SPEED
              );
      };
      

      I'm wondering whether it's redundant to initialize the members both in-line, and in the c'tor. It would seem so, as the c'tor will do the initialization whenever a new object is constructed. But, it seems kind of negligent not to initialize all the members inline.

      What do the experts do?

      Thanks...

      J Offline
      J Offline
      JoeCFD
      wrote on 13 Nov 2023, 18:52 last edited by JoeCFD
      #2

      @mzimmers said in member initialization in definition and c'tor: redundant?:

      Pump(
          QString fwVersion = QStringLiteral(""),
          QString address = QStringLiteral(""),
          ApplicationType application = INVALID_APPLICATION,
          PumpCategory pumpCategory = INVALID_CATEGORY,
          int primeDuration = INVALID_DURATION,
          int primeSpeed = INVALID_SPEED,
          int minSpeed = INVALID_SPEED,
          int maxSpeed = INVALID_SPEED,
          int freezeSpeed = INVALID_SPEED
          );
      

      if you have something following in a class:

         Pump::Pump(
              QString fwVersion = QStringLiteral(""),
              QString address = QStringLiteral(""),
              ApplicationType application = INVALID_APPLICATION,
              PumpCategory pumpCategory = INVALID_CATEGORY,
              int primeDuration = INVALID_DURATION,
              int primeSpeed = INVALID_SPEED,
              int minSpeed = INVALID_SPEED,
              int maxSpeed = INVALID_SPEED,
              int freezeSpeed = INVALID_SPEED
              ):   m_fwVersion{ fwVersion  },
                  m_address{ address  }, (...all...)
                  m_freezeSpeed{ freezeSpeed }
      {
      } 
      

      the following initialization is redundant.

          QString m_fwVersion = QStringLiteral("");
          QString m_address = QStringLiteral("");
          ApplicationType m_application = INVALID_APPLICATION;
          PumpCategory m_pumpCategory = INVALID_CATEGORY;
          int m_primeDuration = INVALID_DURATION;
          int m_primeSpeed = INVALID_SPEED;
          int m_minSpeed = INVALID_SPEED;
          int m_maxSpeed = INVALID_SPEED;
          int m_freezeSpeed = INVALID_SPEED;
      
      M S 2 Replies Last reply 13 Nov 2023, 22:51
      1
      • J JoeCFD
        13 Nov 2023, 18:52

        @mzimmers said in member initialization in definition and c'tor: redundant?:

        Pump(
            QString fwVersion = QStringLiteral(""),
            QString address = QStringLiteral(""),
            ApplicationType application = INVALID_APPLICATION,
            PumpCategory pumpCategory = INVALID_CATEGORY,
            int primeDuration = INVALID_DURATION,
            int primeSpeed = INVALID_SPEED,
            int minSpeed = INVALID_SPEED,
            int maxSpeed = INVALID_SPEED,
            int freezeSpeed = INVALID_SPEED
            );
        

        if you have something following in a class:

           Pump::Pump(
                QString fwVersion = QStringLiteral(""),
                QString address = QStringLiteral(""),
                ApplicationType application = INVALID_APPLICATION,
                PumpCategory pumpCategory = INVALID_CATEGORY,
                int primeDuration = INVALID_DURATION,
                int primeSpeed = INVALID_SPEED,
                int minSpeed = INVALID_SPEED,
                int maxSpeed = INVALID_SPEED,
                int freezeSpeed = INVALID_SPEED
                ):   m_fwVersion{ fwVersion  },
                    m_address{ address  }, (...all...)
                    m_freezeSpeed{ freezeSpeed }
        {
        } 
        

        the following initialization is redundant.

            QString m_fwVersion = QStringLiteral("");
            QString m_address = QStringLiteral("");
            ApplicationType m_application = INVALID_APPLICATION;
            PumpCategory m_pumpCategory = INVALID_CATEGORY;
            int m_primeDuration = INVALID_DURATION;
            int m_primeSpeed = INVALID_SPEED;
            int m_minSpeed = INVALID_SPEED;
            int m_maxSpeed = INVALID_SPEED;
            int m_freezeSpeed = INVALID_SPEED;
        
        M Offline
        M Offline
        mzimmers
        wrote on 13 Nov 2023, 22:51 last edited by
        #3

        @JoeCFD yeah, that's what I figured. Thanks for the confirmation.

        1 Reply Last reply
        0
        • M mzimmers has marked this topic as solved on 13 Nov 2023, 22:51
        • J JoeCFD
          13 Nov 2023, 18:52

          @mzimmers said in member initialization in definition and c'tor: redundant?:

          Pump(
              QString fwVersion = QStringLiteral(""),
              QString address = QStringLiteral(""),
              ApplicationType application = INVALID_APPLICATION,
              PumpCategory pumpCategory = INVALID_CATEGORY,
              int primeDuration = INVALID_DURATION,
              int primeSpeed = INVALID_SPEED,
              int minSpeed = INVALID_SPEED,
              int maxSpeed = INVALID_SPEED,
              int freezeSpeed = INVALID_SPEED
              );
          

          if you have something following in a class:

             Pump::Pump(
                  QString fwVersion = QStringLiteral(""),
                  QString address = QStringLiteral(""),
                  ApplicationType application = INVALID_APPLICATION,
                  PumpCategory pumpCategory = INVALID_CATEGORY,
                  int primeDuration = INVALID_DURATION,
                  int primeSpeed = INVALID_SPEED,
                  int minSpeed = INVALID_SPEED,
                  int maxSpeed = INVALID_SPEED,
                  int freezeSpeed = INVALID_SPEED
                  ):   m_fwVersion{ fwVersion  },
                      m_address{ address  }, (...all...)
                      m_freezeSpeed{ freezeSpeed }
          {
          } 
          

          the following initialization is redundant.

              QString m_fwVersion = QStringLiteral("");
              QString m_address = QStringLiteral("");
              ApplicationType m_application = INVALID_APPLICATION;
              PumpCategory m_pumpCategory = INVALID_CATEGORY;
              int m_primeDuration = INVALID_DURATION;
              int m_primeSpeed = INVALID_SPEED;
              int m_minSpeed = INVALID_SPEED;
              int m_maxSpeed = INVALID_SPEED;
              int m_freezeSpeed = INVALID_SPEED;
          
          S Offline
          S Offline
          SimonSchroeder
          wrote on 15 Nov 2023, 08:10 last edited by
          #4

          @JoeCFD said in member initialization in definition and c'tor: redundant?:

          the following initialization is redundant.

          And it will eventually get out of sync. This is only true as long as you only have this one constructor (or only copy/move constructors in addition).

          If the constructor does nothing else than just initialize the member, you can ditch the constructor all together. This would then even allow (with C++20) to use aggregate initialization: https://en.cppreference.com/w/cpp/language/aggregate_initialization (which would be better than default parameters in a constructor as some members could be skipped and it would be more expressive).

          1 Reply Last reply
          0

          2/4

          13 Nov 2023, 18:52

          • Login

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