static-members

Why might a static data member not get initialized?

霸气de小男生 提交于 2019-12-05 07:45:34
I'm trying to register a bunch of classes with a factory at load time. My strategy is to harness static initialization to make sure that before main() begins, the factory is ready to go. This strategy seems to work when I link my library dynamically, but not when I link statically; when I link statically, only some of my static data members get initialized. Let's say my factory builds Cars. I have CarCreator classes that can instantiate a handful of cars, but not all. I want the factory to collect all of these CarCreator classes so that code looking for a new Car can go to the factory without

What is the best way to initialize a complex static member in Java?

限于喜欢 提交于 2019-12-05 06:47:33
问题 My objective is to have a private static Properties object in my class, to act as defaults when creating other Properties objects needed by my application. The current implementation looks like this: public class MyClass { private static Properties DEFAULT_PROPERTIES = new Properties(); static { try { DEFAULT_PROPERTIES.load( MyClass.class.getResourceAsStream("myclass.properties")); } catch (IOException e) { throw new RuntimeException(e); } } } Looking at it, it works, but it doesn't feel

Static initialization of inherited static member

一笑奈何 提交于 2019-12-05 06:43:09
Consider this example code: public class A<T> { public static T TheT { get; set; } } public class B : A<string> { static B() { TheT = "Test"; } } public class Program { public static void Main(String[] args) { Console.WriteLine(B.TheT); } } Here B.TheT is null. However, changing the Main method like this: public static void Main() { new B(); Console.WriteLine(B.TheT); } B.TheT is "Test", as expected. I can understand that this forces the static constructor to run, but why does this not happen for the first case? I tried reading the spec, and this caught my attention (§10.12): [...] The

accessing static member from non-static function in typescript

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 06:02:01
I am trying to access a static member from a non-static function in the class, and I get an error saying Static member cannot be accessed off an instance variable this is how my code looks - class myClass { public static testStatic: number = 0; public increment(): void { this.testStatic++; } } From what I understand of static members/methods, we shouldn't access non-static members in static functions, but vice-versa should be possible. the static member is already created and is valid, so why can't I access from my non-static method? Access static members from inside the class the same way you

Is a class with only static methods preferable to a namespace?

烂漫一生 提交于 2019-12-05 05:54:54
I was inspired by the comments under this question . I didn't see any reason why a class with only static functions would be a better design than a namespace (with just functions). Any list of pros and cons of these two approaches are welcomed. It would be great with some practical examples! One non-stylistic difference is that you can use a class as a template parameter, but you cannot use a namespace. This is sometimes used for policy classes, like std::char_traits . Outside of that use case, I would stick to a namespace with regular functions. Classes with static methods You can have class

Get value of static field

巧了我就是萌 提交于 2019-12-05 05:30:11
I've got the following class: public static class Pages { public static string LoggedOut = "LoggedOut.aspx"; public static string Login = "Login.aspx"; public static string Home = "Home.aspx"; } I know I can use Pages.Home statically, but there is a reason for my question. I wish to have a method that I can call like this: string pageName = Pages.GetPage("Home"); etc. C'est possible? Thanks, Dave You can use the following: var field = typeof(Pages).GetField("Home", BindingFlags.Public | BindingFlags.Static); var value = (string)field.GetValue(null); You can do it like Konrad suggested using

Static members of static libraries

一个人想着一个人 提交于 2019-12-05 02:00:06
问题 I have static library with static member. This library statically linked to main application and to one of its plugins. Looks like static variable initializing both in main (application) and in dll (plugin). Question : How to avoid static variable reinitialization when dynamic library loading. Or may be I missing something simple? More information: This is simple static library, that contains static member and it's getter and setter: orbhelper.h class ORBHelper { static std::string sss_;

Proper initialization of static constexpr array in class template?

不问归期 提交于 2019-12-05 00:32:12
Static class members in C++ have caused a little confusion for me due to the standard's verbiage: 9.4.2 Static data members [class.static.data] The declaration of a static data member in its class definition is not a definition... However a constexpr is required to be initialized (AFAIK, couldn't find a quote from the standard) at its declaration (e.g., in the class definition). Because of the restrictions on constexpr I had actually forgotten about the requisite for static members to be defined outside of the class, until I tried accessing a static constexpr array. This related question

Why does C# compiler overload resolution algorithm treat static and instance members with equal signature as equal?

十年热恋 提交于 2019-12-04 23:26:28
Let we have two members equal by signature, but one is static and another - is not: class Foo { public void Test() { Console.WriteLine("instance"); } public static void Test() { Console.WriteLine("static"); } } but such code generate brings a compiler error: Type 'Foo' already defines a member called 'Test' with the same parameter types But why? Let we compiled that successfully, then: Foo.Test() should output "static" new Foo().Test(); should output "instance" Can't call the static member instead of instance one because in this case another, more reasonable compiler error will occur: Member

Class-scoped enum

百般思念 提交于 2019-12-04 23:10:32
I have a c++ class with an enum inside, and I wanted to mimick that with boost::python , so that I can write MyClass.value in python. boost::python::class_ does not have an enum_ method, and I was looking for workarounds. I first tried with lambdas like MyClass{ enum{value1,value2}; }; class_<MyClass>("MyClass").add_property("value1",&[](){return value1;}).staticmethod("value1"); which gives compiler error (in get_signature for add_property ). I know I could create getter method for each of the values, but that seems very awkward to me (typing-wise). Using attr : auto classObj=class_<MyClass>(