static-members

struct static member meaning/definition [closed]

匆匆过客 提交于 2019-12-01 01:24:37
struct a{static int z;}l; (a is declared at file scope) I cant initialize the z using a initializer list. what does a static struct member mean? does z(name) have external linkage and public access as well? (I thought it meant you give it file scope and group it under a(and has public access through a object)?..why cant I initialize?) Also....what If I had a static struct member in a class? static member of a class / struct is a member that is not specific for a concrete instance of that class / struct . Apart from some special cases, it must almost always be explicitly initialized in one of

Static template data members storage

删除回忆录丶 提交于 2019-12-01 00:48:24
First I'll write example to properly address the question. First of all, I'll declare template to be used to create singleton object (not auto-created): singleton_base.h template <class Derived> class SingletonBase { public: static Derived* instance() { assert(s_instance); return dynamic_cast<Derived*>(s_instance); } protected: SingletonBase() { assert(s_instance==0); s_instance=this; } virtual ~SingletonBase() { assert(s_instance); s_instance=0; } private: static SingletonBase* s_instance; }; template <class Derived> SingletonBase<Derived>* SingletonBase<Derived>::s_instance = 0; Now we can

need of Static variables and their overhead on jvm

荒凉一梦 提交于 2019-12-01 00:41:31
As per the concept about static members, they are created/loaded into the memory when there is first call made to its class. And they are common among all instances of that class. Means they are not re-created or re-itialized etc. In addition, They can be accessed by the class name only. There is no need to create object for that class just to access them. Now my questions are; Whether static members ever be in memory till the application is running? even if all the instances of that class had been collected by GC(garbage collector). For a big project, where 8-10 teams are working together,

Why aren't static data members allowed in local classes?

旧时模样 提交于 2019-11-30 21:36:10
问题 What is the reasoning to why static const members cannot exist in local classes? It seems like a rather silly restriction. Example: void foo() { struct bar { int baz() { return 0; } // allowed static const int qux = 0; // not allowed?!? }; } struct non_local_bar { int baz() { return 0; } // allowed static const int qux = 0; // allowed }; Quote from standard (9.8.4): A local class shall not have static data members. 回答1: From the standard section 9.4.2: If a static data member is of const

Call a function before static member allocation

时光怂恿深爱的人放手 提交于 2019-11-30 21:15:41
问题 I am using a 3rd party API that overrides the memory management functions found in the C Runtime libraries. In order for everything to work properly, I must make a call to initialize the API before any memory allocations take place. The project I am working on uses a static Factory object that is dynamically initialized before any of the code in the main file is executed. How can I ensure that the API will be initialized before the static Factory object? 回答1: The C++ standard library runs

Passing static parameters to a class

瘦欲@ 提交于 2019-11-30 21:02:42
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? 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 check that 'bar' is already initialized (or not) as appropriate. You can't pass parameters to a static

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

醉酒当歌 提交于 2019-11-30 19:26:30
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_; }; class A { public: static B b_; }; template<typename T> B Atempl<T>::b_("Atempl"); B A::b_("A");

Static template data members storage

99封情书 提交于 2019-11-30 19:04:18
问题 First I'll write example to properly address the question. First of all, I'll declare template to be used to create singleton object (not auto-created): singleton_base.h template <class Derived> class SingletonBase { public: static Derived* instance() { assert(s_instance); return dynamic_cast<Derived*>(s_instance); } protected: SingletonBase() { assert(s_instance==0); s_instance=this; } virtual ~SingletonBase() { assert(s_instance); s_instance=0; } private: static SingletonBase* s_instance; }

Using Static method and variables - Good vs Bad

依然范特西╮ 提交于 2019-11-30 18:54:07
I am developing C# and asp.net web application. I have general class called utilities, I have lot of public and static variables in this public utilities class. Since this number is gradually increasing, I want to know is it good practice to store utilities methods and variable as public static. Example of my code public class utilities { public static string utilVariable1 = "Myvalue"; public static string utilVariable2 = "Myvalue"; public static string utilVariable3 = "Myvalue"; : public static string utilVariableN = "Myvalue"; public static string UtilMethod1() { //do something } public

Java final static declarations in method local classes

人盡茶涼 提交于 2019-11-30 14:41:58
问题 When declaring a local inner class inside a method, why is it legal to include final static Strings or ints but not legal to include other objects? For instance: class Outer { void aMethod() { class Inner { final static String name = "compiles"; final static int ctr = 10; // compiles final static Integer intThree = Integer.valueOf(3); // does not compile! final static obj objConst = new Object(); // does not compile! } Inner inner = new Inner(); } } When I compile this, I get the following: