How do you initialize static data members, similar to static constructors? [duplicate]

To get the equivalent of a static constructor, you need to write a separate ordinary class to hold the static data and then make a static instance of that ordinary class. class StaticStuff { std::vector<char> letters_; public: StaticStuff() { for (char c=”a”; c <= ‘z’; c++) letters_.push_back(c); } // provide some way to get at … Read more

What Is a Curly-Brace Enclosed List If Not an intializer_list?

It is an braced-init-list. A braced-init-list existed before std::initializer_list and is used to initialize aggregates. int arr[] = {1,2,3,4,5}; The above used a braced-init-list to initialize the array, no std::initializer_list is created. On the other hand when you do std::vector<int> foo = {1,2,3,4,5}; foo is not an aggregate so the braced-init-list is used to create … Read more

Subclassing NSWindowController in Swift and init(windowNibName)

Instead of overriding any of the init methods you can simply override the windowNibName property and return a hardcoded string. This allows you to call the basic vanilla init method to create the window controller. class WindowController: NSWindowController { override var windowNibName: String! { return “NameOfNib” } } let windowController = WindowController() I prefer this … Read more

Is it possible to use a c# object initializer with a factory method?

No. Alternatively you could accept a lambda as an argument, which also gives you full control in which part of the “creation” process will be called. This way you can call it like: MyClass instance = MyClass.FactoryCreate(c=> { c.SomeProperty = something; c.AnotherProperty = somethingElse; }); The create would look similar to: public static MyClass FactoryCreate(Action<MyClass> … Read more

How to satisfy a protocol which includes an initializer?

Ok, my bad. To guarantee that all subclasses conform to MyProtocol new initializer has to be marked as required as well. Furthermore Swift requires to declare all required initializers directly within the class and does not allow to declare them in extensions. extension MyClass : MyProtocol { required convenience init(name: String) { self.init() self.name = … Read more

static readonly field initializer vs static constructor initialization

There is one subtle difference between these two, which can be seen in the IL code – putting an explicit static constructor tells the C# compiler not to mark the type as beforefieldinit. The beforefieldinit affects when the type initializer is run and knowing about this is useful when writing lazy singletons in C#, for … Read more