initialization

Differences between direct-list-initialization and copy-list-initialization

佐手、 提交于 2020-08-18 00:05:32
问题 I would like to know if there is any difference the following two types of std::vector initialization in C++11 and later. std::vector<int> v1 {1, 2, 3, 4, 5}; std::vector<int> v2 = {1, 2, 3, 4, 5}; Here is a complete code example that works fine. #include <iostream> #include <vector> int main() { std::vector<int> v1 {1, 2, 3, 4, 5}; std::vector<int> v2 = {1, 2, 3, 4, 5}; std::cout << v1.size() << '\n'; std::cout << v2.size() << '\n'; } I see both initializations leading to identical results.

How to initialize List/Map as a default argument?

允我心安 提交于 2020-08-09 09:10:38
问题 I know how to initialize other variables such as int or String in constructor but I have no clue how to do it for List and Map. class StackOverFlowQuestion{ StackOverFlowQuestion({this.test='', this.map=?, this.list=?}); String test; Map map; List list; } What shall I replace the question marks with? Thanks. 回答1: The answer depends on whether your default list and map are constant. Let's assume your list is, but map is not. You'd write: class StackOverFlowQuestion { StackOverFlowQuestion({

Why can braced-init-list not be used in ternary operator?

余生颓废 提交于 2020-07-28 08:48:47
问题 My compiler is the latest VC++ 2013 RC. int f(bool b) { return {}; // OK return b ? 1 : { }; // C2059: syntax error : '{' return b ? 1 : {0}; // C2059: syntax error : '{' return b ? {1} : {0}; // C2059: syntax error : '{' } Why can braced-init-list not be used in ternary operator? Is this behavior defined as ill-formed by the C++ standard, or just a bug of the VC++ compiler? 回答1: Well, here's what the standard says about the braced-init-list (8.5.3.1): List-initialization can be used as the

Can “T t = {};” and “T t{};” produce different results?

谁说胖子不能爱 提交于 2020-07-17 07:39:10
问题 The question is simple. Is it possible to construct such a type T, for which the following two variables declarations will produce different results? T t1 = {}; T t2{}; I've been digging through the cppreference and the standard for more than an hour now, and I understood the following: T t2{}; is a value initialization. No surprises here. T t1 = {} is a list initialization with an empty braced-init-list. But the last one is tricky since the "effects of list initialization" is an impressive..

Compact syntax for instantiating an initializing collection

和自甴很熟 提交于 2020-07-15 04:14:30
问题 I'm looking for a compact syntax for instantiating a collection and adding a few items to it. I currently use this syntax: Collection<String> collection = new ArrayList<String>(Arrays.asList(new String[] { "1", "2", "3" })); I seem to recall that there's a more compact way of doing this that uses an anonymous subclass of ArrayList , then adds the items in the subclass' constructor. However, I can't seem to remember the exact syntax. 回答1: http://blog.firdau.si/2010/07/01/java-tips-initializing

Compact syntax for instantiating an initializing collection

时光毁灭记忆、已成空白 提交于 2020-07-15 04:14:25
问题 I'm looking for a compact syntax for instantiating a collection and adding a few items to it. I currently use this syntax: Collection<String> collection = new ArrayList<String>(Arrays.asList(new String[] { "1", "2", "3" })); I seem to recall that there's a more compact way of doing this that uses an anonymous subclass of ArrayList , then adds the items in the subclass' constructor. However, I can't seem to remember the exact syntax. 回答1: http://blog.firdau.si/2010/07/01/java-tips-initializing

Compact syntax for instantiating an initializing collection

穿精又带淫゛_ 提交于 2020-07-15 04:14:05
问题 I'm looking for a compact syntax for instantiating a collection and adding a few items to it. I currently use this syntax: Collection<String> collection = new ArrayList<String>(Arrays.asList(new String[] { "1", "2", "3" })); I seem to recall that there's a more compact way of doing this that uses an anonymous subclass of ArrayList , then adds the items in the subclass' constructor. However, I can't seem to remember the exact syntax. 回答1: http://blog.firdau.si/2010/07/01/java-tips-initializing

C++ Rules of Initialization - Constructor before static members?

筅森魡賤 提交于 2020-07-03 05:43:25
问题 I had a value that I decided could be static. I decided to initialize the default on the definition, but the Constructor would load in any saved changes from last run (say static function Load() ). Only finding that the constructor is called before static initialization so Load() changes get lost. Example below, we end up with m_var 7 instead of 2. class x { public: x() { Load(); }; static void Load(); static int m_var; }; int x::m_var=7; void x::Load() { m_var=2; } class y { public: static x

C++ Rules of Initialization - Constructor before static members?

大憨熊 提交于 2020-07-03 05:43:16
问题 I had a value that I decided could be static. I decided to initialize the default on the definition, but the Constructor would load in any saved changes from last run (say static function Load() ). Only finding that the constructor is called before static initialization so Load() changes get lost. Example below, we end up with m_var 7 instead of 2. class x { public: x() { Load(); }; static void Load(); static int m_var; }; int x::m_var=7; void x::Load() { m_var=2; } class y { public: static x

Difference of value of **(ptr+1) in char and int

こ雲淡風輕ζ 提交于 2020-06-29 05:05:25
问题 Please help me out as to why in below code outputs are different: int z[][3] = { 1, 2, 3, 4, 5, 6 }; printf("\n**(z+1): %d", **(z + 1)); Output: (z+1): 4 char y[][3] = { "A", "F", "G", "J", "M", "P" }; printf("\n**(y+1): %c", **(y+1)); Output: (y+1): F Why in the above two outputs, first is checking the 4th index while in second output prints 2nd index ? 回答1: Why in the above two outputs, first is checking the 4th index while in second output prints 2nd index ? That's not actually close to