initializer

C++: constructor initializer for arrays

折月煮酒 提交于 2019-11-26 05:58:33
问题 I\'m having a brain cramp... how do I initialize an array of objects properly in C++? non-array example: struct Foo { Foo(int x) { /* ... */ } }; struct Bar { Foo foo; Bar() : foo(4) {} }; array example: struct Foo { Foo(int x) { /* ... */ } }; struct Baz { Foo foo[3]; // ??? I know the following syntax is wrong, but what\'s correct? Baz() : foo[0](4), foo[1](5), foo[2](6) {} }; edit: Wild & crazy workaround ideas are appreciated, but they won\'t help me in my case. I\'m working on an

Use of Initializers vs Constructors in Java

和自甴很熟 提交于 2019-11-26 03:29:23
问题 So I\'ve been brushing up on my Java skills as of late and have found a few bits of functionality that I didn\'t know about previously. Static and Instance Initializers are two such techniques. My question is when would one use an initializer instead of including the code in a constructor? I\'ve thought of a couple obvious possibilities: static/instance initializers can be used to set the value of \"final\" static/instance variables whereas a constructor cannot static initializers can be used

Why are C# 3.0 object initializer constructor parentheses optional?

被刻印的时光 ゝ 提交于 2019-11-26 03:17:31
问题 It seems that the C# 3.0 object initializer syntax allows one to exclude the open/close pair of parentheses in the constructor when there is a parameterless constructor existing. Example: var x = new XTypeName { PropA = value, PropB = value }; As opposed to: var x = new XTypeName() { PropA = value, PropB = value }; I\'m curious why the constructor open/close parentheses pair is optional here after XTypeName ? 回答1: This question was the subject of my blog on September 20th 2010. Josh and Chad

static constructors in C++? I need to initialize private static objects

强颜欢笑 提交于 2019-11-26 02:27:42
问题 I want to have a class with a private static data member (a vector that contains all the characters a-z). In java or C#, I can just make a \"static constructor\" that will run before I make any instances of the class, and sets up the static data members of the class. It only gets run once (as the variables are read only and only need to be set once) and since it\'s a function of the class it can access its private members. I could add code in the constructor that checks to see if the vector