Questions regarding initialization code of static members
Questions tagged [static-initialization]
325 questions
5
votes
2 answers
c++ string constant and static initialization order fiasco
I'm trying to understand when the static initialization order fiasco is a real problem. If I use a string constant like kName below would it suffer any of the problems of the static initialization order fiasco? Is this a problem in this case because…

Mike Sweeney
- 1,896
- 2
- 18
- 20
5
votes
6 answers
Imitate a static constructor in C++
This a question related to the initialization of objects in C++.
I have a group of classes (not instances), inheriting from a common base class, and I need them to register info about themselves in a container (specifically a map) when the program…

Chris
- 419
- 5
- 13
5
votes
6 answers
cannot override static initialization in derived class
i'm trying to provide different static initializations for classes in a hierarchy, but when i tried with this code:
#include
using namespace std;
struct base {
static const char* componentName;
};
const char* base::componentName =…

lurscher
- 25,930
- 29
- 122
- 185
5
votes
1 answer
Does static initialization (and/or other) code get run when dlopen'ing?
When you dlopen() a shared object, is there a mechanism for having code in that DLL execute without being called explicitly? Specifically, C++ static initialization code for globals/statics which the caller of dlopen() might not know about? I'm…

einpoklum
- 118,144
- 57
- 340
- 684
5
votes
1 answer
How to comprehend that an implementation is permitted to treat dynamic initialization of non-local variable as static initialization in some cases?
In fact, the problem comes from the words in the standard draft N4582:
[basic.start.static/3] An implementation is permitted to perform the initialization of a variable with static or thread storage duration as a static initialization even if such…

xskxzr
- 12,442
- 12
- 37
- 77
5
votes
3 answers
This Java Program is always printing only 10 but not printing SB.Why?
public class Test {
public static void main(String[] args) {
System.out.println(Hello.a1);
}
}
class Hello {
static final int a1=10;
static {
System.out.println("SB");
}
}
This code is always printing 10 but…

chandankumar patra
- 109
- 1
- 1
- 5
5
votes
3 answers
Java - Class type from inside static initialization block
Is it possible to get the class type from inside the static initialization block?
This is a simplified version of what I currently have::
class Person extends SuperClass {
String firstName;
static{
// This function is on the…

Chris Dutrow
- 48,402
- 65
- 188
- 258
5
votes
1 answer
Static field initializer is not called in Windows Phone 8 C# app
I have a static class with a static field that is initialized in place:
private static SomeType _instance = new SomeType();
This code is a part of a portable class library that is used on multiple platforms. Everything works fine on desktop…

Vagif Abilov
- 9,835
- 8
- 55
- 100
5
votes
1 answer
objective c - is local static variable initialization thread safe?
note: i'm using objective-c++ where non-compile-time constant is allowed (https://stackoverflow.com/a/12304815/3101492)
+ (Foo)sharedFoo
{
static Foo *foo = [Foo new];
return foo;
}
static initializer is expected to be run only once, but…

jason na
- 342
- 1
- 10
5
votes
1 answer
Initialize static class implicitly
is it possible to initialize a static class on app start up "automatically"?
By automatically I mean without the need of referencing a property.
The reason I want to be able to do this for is that I'd like to automatically theme an app on start…

pikausp
- 1,142
- 11
- 31
4
votes
1 answer
Static Initialization and Use of a Class in a Separate Module in D
In my program, I have a class that I want to be allocated before entering main(). I'd like to tuck these away in a separate module to keep the clutter out of my code; However, as soon as the module goes out of scope (before main() is entered), the…

Meta
- 1,091
- 6
- 14
4
votes
1 answer
How to avoid deferring the initialization of static storage duration variables?
I have classes with side effects in their constructors, and objects of these classes are global objects that have static storage duration. During the initialization these objects register their classes in a special map, and it is important for these…

Dmitry Kuzminov
- 6,180
- 6
- 18
- 40
4
votes
2 answers
static member explicit definition
Consider this code:
#include
using namespace std;
class Wilma
{
public:
static int i;
Wilma()
{
cout<<"\nWilma ctor\n";
cout<<"\ni::"<

ashishsony
- 2,537
- 3
- 26
- 38
4
votes
1 answer
Why is the address of a nested function (GNU extension) in GCC considered "not constant" by the compiler?
The GNU C compiler contains a nice extension to the C language, called Nested Functions.
However, the documentation is unclear in some points. For example, it says that
It is possible to call the nested function from outside the scope of its name…

Maciek Godek
- 172
- 12
4
votes
3 answers
std::set used as a static templated member variable
I am trying to make something like a Java style Enum, which I'm calling a flag. The requirements are that each flag is static so flags are directly referencable, each flag storing the string of it's name and the whole set iterable and conducive to…

user745247
- 43
- 5