forked from qt-creator/qt-creator
Initializion with Curly Brackets
Curly brackets for initialization got quite powerful. To not invent a different style we should use the some style like for rounded brackets. Change-Id: Id4789fb8d4d7f3980a3a8ce2df999cc57022a2a7 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -900,6 +900,27 @@
|
||||
const QVector<int> values = {1, 2, 3, 4, 5};
|
||||
\endcode
|
||||
|
||||
\section3 Initialization with Curly Brackets
|
||||
|
||||
If you use initialization with curly brackets, follow the same rules as with
|
||||
round brackets. For example:
|
||||
|
||||
\code
|
||||
class Values // the following code is quite useful for test fixtures
|
||||
{
|
||||
float floatValue = 4; // prefer that for simple types
|
||||
QVector<int> values = {1, 2, 3, 4, integerValue}; // prefer that syntax for initializer lists
|
||||
SomeValues someValues{"One", 2, 3.4}; // not an initializer_list
|
||||
SomeValues &someValuesReference = someValues;
|
||||
ComplexType complexType{values, otherValues} // constructor call
|
||||
}
|
||||
|
||||
object.setEntry({"SectionA", value, doubleValue}); // calls a constructor
|
||||
object.setEntry({}); // calls default constructor
|
||||
\endcode
|
||||
|
||||
But be careful not to overuse it, to not obfuscate your code.
|
||||
|
||||
\section3 Non-Static Data Member Initialization
|
||||
|
||||
Use non-static data member initialization for trivial initializations, except in public
|
||||
|
||||
Reference in New Issue
Block a user