initializer

How to handle a static final field initializer that throws checked exception

家住魔仙堡 提交于 2019-11-26 16:04:49
问题 I am facing a use case where I would like to declare a static final field with an initializer statement that is declared to throw a checked exception. Typically, it'd look like this: public static final ObjectName OBJECT_NAME = new ObjectName("foo:type=bar"); The issue I have here is that the ObjectName constructor may throw various checked exceptions, which I don't care about (because I'd know my name is valid, and it's allright if it miserably crashes in case it's not). The java compiler

C++: constructor initializer for arrays

喜夏-厌秋 提交于 2019-11-26 15:21:24
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 embedded processor where std::vector and other STL constructs are not available, and the obvious workaround is to

Static class initializer in PHP

廉价感情. 提交于 2019-11-26 15:14:16
问题 I have an helper class with some static functions. All the functions in the class require a ‘heavy’ initialization function to run once (as if it were a constructor). Is there a good practice for achieving this? The only thing I thought of was calling an init function, and breaking its flow if it has already run once (using a static $initialized var). The problem is that I need to call it on every one of the class’s functions. 回答1: Sounds like you'd be better served by a singleton rather than

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

寵の児 提交于 2019-11-26 14:27:09
问题 I asked a question here: Lifetime Extension of a initializer_list return involving the non-functional code: const auto foo = [](const auto& a, const auto& b, const auto& c) { return {a, b, c}; }; I believed the lambda was trying to return an intializer_list (that's bad, don't do that.) But I got a comment: It's not an initializer_list , it's an initializer list. Two different things. I just thought that any time you did a curly-braced list you were creating an intializer_list . If that's not

CoreData: error: Failed to call designated initializer on NSManagedObject class

风格不统一 提交于 2019-11-26 13:56:14
问题 I have a little damn problem with CoreData. I want to insert a new Object, so I first have to create one. This is done by that code: Challenges *newChallenge = [[Challenges alloc] init]; [newChallenge setName:@"TestChallenge"]; [newChallenge setRounds:[[NSNumber alloc] initWithInt:12]]; [newChallenge setShots:[[NSNumber alloc] initWithInt:5]]; [newChallenge setDate:[NSDate date]]; But however after the alloc init I get this error: CoreData: error: Failed to call designated initializer on

How to create initializer to create and migrate mysql database?

我与影子孤独终老i 提交于 2019-11-26 13:08:30
问题 I have been learning how to use EF for a week or so now and am stuck on the issue of creating/updating my database. I am able to create an initializer to create the database if it is not there: static class Program { static void Main() { Database.SetInitializer<GumpDatabase>(new GumpDatabaseInitializer()); .... class GumpDatabaseInitializer : CreateDatabaseIfNotExists<GumpDatabase> { public GumpDatabaseInitializer() { } protected override void Seed(GumpDatabase context) { context.Database

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

假如想象 提交于 2019-11-26 11:32:59
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 is initialized, and initialize it if it's not, but that introduces many necessary checks and doesn't seem

Can I declare variables of different types in the initialization of a for loop? [duplicate]

浪尽此生 提交于 2019-11-26 11:13:13
问题 This question already has answers here : Is it possible to declare two variables of different types in a for loop? (8 answers) Closed 3 years ago . Why does this C++ code not compile under VS2010: for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {} while this one does: short b = 0; for ( int a = 0; a < 10; ++a, ++b ) {} Is the declaration of two variables of different types inside the for-loop initializer prohibited? If so, how can you work around it? 回答1: Yes, that is prohibited. Just as

Calling a Java method with no name

依然范特西╮ 提交于 2019-11-26 07:57:16
问题 I\'m looking at the code below and found something a bit strange: public class Sequence { Sequence() { System.out.print(\"c \"); } { System.out.print(\"y \"); } public static void main(String[] args) { new Sequence().go(); } void go() { System.out.print(\"g \"); } static { System.out.print(\"x \"); } } I would\'ve expected this to give a compilation error as the System.out with \"y \" doesn\'t belong to a method declaration just a { } . Why is this valid? I don\'t see how this code would or

Is an empty initializer list valid C code?

梦想与她 提交于 2019-11-26 06:40:56
问题 It is common to use {0} to initialize a struct or an array but consider the case when the first field isn\'t a scalar type. If the first field of struct Person is another struct or array, then this line will result in an error ( error: missing braces around initializer ). struct Person person = {0}; At least GCC allows me to use an empty initializer list to accomplish the same thing struct Person person = {}; But is this valid C code? Also: Is this line guaranteed to give the same behavior, i