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 Update on Monday, May 27th 2025

member initialization in definition and c'tor: redundant?

Scheduled Pinned Locked Moved Solved C++ Gurus
4 Posts 3 Posters 445 Views
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on 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...

    JoeCFDJ 1 Reply Last reply
    0
    • mzimmersM mzimmers

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

      JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on 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;
      
      mzimmersM S 2 Replies Last reply
      1
      • JoeCFDJ JoeCFD

        @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;
        
        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • mzimmersM mzimmers has marked this topic as solved on
        • JoeCFDJ JoeCFD

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

          • Login

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