Up until now, I have been initializing most of my variables as such:
const QString foo("bar");
Which from my newfound understanding, is known as direct initialization. (http://en.cppreference.com/w/cpp/language/direct_initialization)
I was looking at someone's code, and in a namespace, they were initializing their variables with Curly Braces:
const QString foo{"bar"};
which also counts as direct initialization:
I never knew this was a thing until today.
Code is more often read than written, what does direct initialization with Curly Braces imply as compared to Parentheses?
Just as I understand a const
will tell me that a variable will not change, can I infer anything from this curly brace practice?
Best Answer
This shows that the author is using modern C++ (e.g. >= C++11) and applies good practice:
You have to be aware that there might however be a subtlety if a class has a constructor from an initializer list.
If you're not familiar with this syntax, I'd really recommend you to read Scott Meyer's above-mentioned book or acquire equivalent knowledge for example with Herb Sutter's videos, in order not to end up with outdated C++ skills.