Questions regarding initialization code of static members
Questions tagged [static-initialization]
292 questions
6
votes
2 answers
Why is the order of destruction of these function-local static objects NOT the inverse of their order of initialization?
I have two function-local static objects, One and Two. One's constructor and destructor both access Two through GetTwo():
#include
struct One;
struct Two;
const One& GetOne();
const Two& GetTwo();
struct Two {
const char* value =…
![](../../users/profiles/3217070.webp)
bberg
- 197
- 7
6
votes
1 answer
Legitimate uses for static initializer?
I remember a couple years ago I was using static initializers to call class-level setup operations. I remember it having very bizarre behaviors and I just decided to steer clear from them. Maybe it was because I was messing up the top-bottom order…
![](../../users/profiles/1373258.webp)
tmn
- 9,565
- 10
- 49
- 99
6
votes
2 answers
Indirect recursion, dependent static variables
Is the result of the following indirect recursion defined by the standard or is it undefined behavior?
auto abc() -> int ;
auto xyz() -> int {
static int instance = 3 + abc();
return instance;
}
auto abc() -> int {
static int instance…
![](../../users/profiles/507433.webp)
MFH
- 1,584
- 2
- 16
- 36
6
votes
2 answers
Thread-safety of static initializers in C#
Everyone says static initializers are thread-safe, but I'm worried about a particular detail.
Let's say I have
static class MyStaticClass
{
public static readonly object myField = MyOtherClass.GetNewObject();
}
static class MyOtherClass
{
…
![](../../users/profiles/541686.webp)
user541686
- 189,354
- 112
- 476
- 821
6
votes
2 answers
Default value for struct parameter
Let's say I have the following struct:
struct myStruct
{
int x;
int y;
int z;
int w;
};
I want to initialize this struct to a default value when calling the following function. If it helps I'm looking for a simple zero initialization.
void…
![](../../users/profiles/279982.webp)
atoMerz
- 6,868
- 14
- 56
- 99
6
votes
4 answers
How to circumvent the size limit of a static initialiser in Java when initialising large amounts of constants
I have a class holding a large a mount of generated constants as such:
public class Constants extends SomeBaseClass {
// init() is defined in some base class...
public static final XXX KEY1 = init(...);
public static final XXX KEY2 =…
![](../../users/profiles/521799.webp)
Lukas Eder
- 181,694
- 112
- 597
- 1,319
5
votes
2 answers
Is the order of file-level static variables always the same within a given translation unit?
I have a program split up into two source files:
example.cpp
#include
class A {
public:
A(int x) {
::std::cout << "In A(" << x << ")\n";
}
};
static A first(1);
static A second(2);
example__main.cpp
int main(int argc,…
![](../../users/profiles/167958.webp)
Omnifarious
- 50,447
- 15
- 117
- 181
5
votes
2 answers
C++ is it possible to delay initialization of constant static member?
I am using Qt but this is a generic C++ question. My case is simple, I have a class Constants which has a constant static member which I want it to be initialized after certain function calls are made.
Constants.h
#ifndef CONSTANTS_H
#define…
![](../../users/profiles/878361.webp)
destan
- 4,042
- 2
- 32
- 55
5
votes
2 answers
Can "construct on first use" idiom fail under any circumstances?
I'm building my program (tests actually) using some static library.
This library contains one file inside which I have functions like that:
string& GetString() {
static string strFilename;
return strFilename;
}
void PrintToScreen() {
…
![](../../users/profiles/704905.webp)
Piotr Kukielka
- 3,622
- 3
- 29
- 40
5
votes
1 answer
Is a pointer to string literal guaranteed to be initialized before a std::string?
//file1.cpp
extern const char* foo;
std::string bar = foo;
//file2.cpp
const char* foo = "foo";
Is bar guaranteed by the standard to be initialized to "foo"? Or could it be initialized before foo gets set and segfault in the constructor i.e. a…
![](../../users/profiles/8606343.webp)
Artikash-Reinstate Monica
- 1,156
- 1
- 7
- 14
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…
![](../../users/profiles/661190.webp)
Chris
- 391
- 4
- 12
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 =…
![](../../users/profiles/170521.webp)
lurscher
- 23,085
- 26
- 113
- 178
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…
![](../../users/profiles/1593077.webp)
einpoklum
- 86,754
- 39
- 223
- 453
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…
![](../../users/profiles/5101782.webp)
chandankumar patra
- 89
- 1
- 5
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…
![](../../users/profiles/223675.webp)
Vagif Abilov
- 9,279
- 5
- 52
- 91