Object, which is going to be constructed, has several constant. What of ways to place constants is better and why?
The first way is function for each member:
BarcodeObject::BarcodeObject() : WareRelatedObject("BARCODE") { createBarcodeItem(); createFactorItem(); } void BarcodeObject::createBarcodeItem() { QString caption = tr("Штрихкод"); QString sqlbind = "BARCODE.BARCODE"; mBarcode = new StringItem(mainGroup(), caption, sqlbind); LineEditConstraint maxLength(40); mBarcode->setConstraint(QVariant::fromValue(maxLength)); } void BarcodeObject::createFactorItem() { QString sqlbind = "BARCODE.FACTOR"; QString caption = tr("Коэффициент"); mFactor = new ValueNumericItem(mainGroup(), caption, sqlbind); PosNumeric defaultValue("1"); NumericSpinBoxConstraint constraint = NumericSpinBoxConstraint::valueConstraint(defaultValue); mFactor->setConstraint(QVariant::fromValue(constraint)); }The second way is to place all constants to namespace:
namespace { const QString barcodeCaption = QObject::trUtf8("Штрихкод"); const QString barcodeSqlBind = "BARCODE.BARCODE"; const LineEditConstraint barcodeConstraint(40); const QString factorCaption = QObject::trUtf8("Коэффициент"); const QString factorSqlBind = "BARCODE.FACTOR"; const NumericSpinBoxConstraint factorConstraint = NumericSpinBoxConstraint::valueConstraint(PosNumeric("1")); } BarcodeObject::BarcodeObject() : WareRelatedObject("BARCODE") { mBarcode = new StringItem(mainGroup(), barcodeCaption, barcodeSqlBind); mBarcode->setConstraint(QVariant::fromValue(barcodeConstraint)); mFactor = new ValueNumericItem(mainGroup(), factorCaption, factorSqlBind); mFactor->setConstraint(QVariant::fromValue(factorConstraint)); }