member initialization in definition and c'tor: redundant?
-
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...
-
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...
@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;
-
@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 mzimmers has marked this topic as solved on
-
@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;
@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).