static-members

When is a static nested class (and static members therein) loaded into memory?

你。 提交于 2019-12-21 07:58:12
问题 Here, I was trying to implement a singleton class for my Database connectivity using the inner static helper class : package com.myapp.modellayer; public class DatabaseConnection { private DatabaseConnection() { //JDBC code... } private static class ConnectionHelper { // Instantiating the outer class private static final DatabaseConnection INSTANCE = new DatabaseConnection(); } public static DatabaseConnection getInstance() { return ConnectionHelper.INSTANCE; } } However, my doubt is when

Singleton class vs. class with static member

左心房为你撑大大i 提交于 2019-12-20 09:45:08
问题 Despite the many threads on that topic, I am still unclear as to when to choose which approach. I am hoping that by discussing a specific example, I will finally "get it." Note: My language here is Cocoa though the general issue is not language-specific. I have a class TaskQueue that I want to use to: access from anywhere in my code in order to add or remove scheduled tasks process automatically the scheduled tasks at regular intervals When TaskQueue is first used, I want TaskQueue to

Why static fields (not final) is restricted in inner class in java [duplicate]

喜欢而已 提交于 2019-12-20 08:42:37
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why does Java prohibit static fields in inner classes? I was going through the specification and got that it is not possible to have the static member in the inner class which is not final compile time constant . class HasStatic { static int j = 100; } class myInnerClassTest { class Inner extends HasStatic { static final int x = 3; // OK: compile-time constant static int y = 4; // Compile-time error: an inner

C++ static data members initialization

心已入冬 提交于 2019-12-20 04:58:13
问题 1) Is it true that static data members of classes always get initialized before main() called? 2) Is it true that "static initialization order fiasco" can happen if static data member of class initialization code uses global static variable of other translation unit? Where can I read more about it? I couldn't find answer in 2003 standard of C++. Thanks a lot. 回答1: 1) Is it true that static data members of classes always get initialized before main() called? yes they would always be

Why we have to define a const static member that is initialized within a class

青春壹個敷衍的年華 提交于 2019-12-20 03:15:22
问题 As we know,It is possible to initialize integral const static members inside the class structure.This is useful when the constant is used in the class structure after the initialization.For example,it can be used as the size of an int array. Look the code following: class MyClass{ static const int num = 100; int elems[num]; ... }; But we still have to define the member num outside the class definition: const int MyClass::num; I don't know why we have to do like this. Could someone tell me why

passing a static constexpr variable by universal reference?

狂风中的少年 提交于 2019-12-19 19:54:57
问题 In the following, static constexpr member L is initialized in-class A and then passed by value or by (universal) reference. The latter fails in Clang but not in GCC, and behaviour is slightly different for member/non-member functions. In more detail: #include <iostream> using namespace std; struct A { static constexpr size_t L = 4; template <typename T> void member_ref(T&& x) { cout << std::forward<T>(x) << endl; } template <typename T> void member_val(T x) { cout << x << endl; } }; template

Sharing static members between template instantiations? (impossible?)

强颜欢笑 提交于 2019-12-19 05:32:52
问题 I am doing something that is probably silly, but it would be nice if it worked. I am attempting to specialize types in a way that I need my own lookup structure that is essentially global (but ideally encapsulated as a class variable), but I want the objects to be type safe, so they are parameterized. Consequently, I have, essentially template<class T, int N> class SpecialArray { //... private: static map<string, internal_t> lookupTable } and for whatever reason, I didn't think until such

Passing static parameters to a class

冷暖自知 提交于 2019-12-19 02:47:31
问题 As far as I know you can can't pass parameters to a static constructor in C#. However I do have 2 parameters I need to pass and assign them to static fields before I create an instance of a class. How do I go about it? 回答1: This may be a call for ... a Factory Method! class Foo { private int bar; private static Foo _foo; private Foo() {} static Foo Create(int initialBar) { _foo = new Foo(); _foo.bar = initialBar; return _foo; } private int quux; public void Fn1() {} } You may want to put a

When do constructors of static members of template classes get called in C++?

依然范特西╮ 提交于 2019-12-18 21:49:14
问题 There is plenty of information on when constructors of static members of ordinary classes are called. However, I am seeing some strange behavior with regard to template classes. What should the output of the following program be? (Note I use printf to avoid any static initialization order fiasco complications with std::cout.) #include <iostream> class B { public: B(const std::string &s) { printf("Hello I am B from %s\n", s.c_str()); } }; template<typename T> class Atempl { public: static B b_

C# - Are Parameters Thread Safe in a Static Method?

蹲街弑〆低调 提交于 2019-12-18 17:02:22
问题 Is this method thread-safe? It seems as though it isn't... public static void Foo(string _str, Guid _id) { _str = _str + _id.ToString(); /* Do Stuff */ return } 回答1: The parameters are, in this case, two immutable values. Within a method, only a single thread is operating on the set of parameters, as multiple threads calling the method will each have their own stack and execution context, which means each thread has their own independent set of parameters and local variables, so no other