Questions regarding initialization code of static members
Questions tagged [static-initialization]
325 questions
0
votes
1 answer
Do we have a static initialisation order fiasco while initialising variables inside the class
I am trying to refactor our old code from c++98 to c++14. And the need to compile with both older gcc(c++98) and newer gcc(c++14). This is how our older code looks like (This is completely made up example, of our complicated code. The static consts…

BhanuKiran
- 2,631
- 3
- 20
- 36
0
votes
0 answers
Static variable from a static linked lib used before its construction
I was trying to create a unit test framework for myself, and it worked fine until I made it into a static lib. The static container unit_base::_units is used before its construction.
I reproduced the issue with the following simpler…

Eugene
- 111
- 4
0
votes
1 answer
Initialize static variables of Interface via Enum
I am trying to understand the below behavior, all I am trying is to initialize the static variable of interface via enum method.
enum Hello {
ProfileResolver();
public Hello resolve() {
System.out.println("resolve method…

SBhogal
- 119
- 2
- 15
0
votes
0 answers
Can I use a global object before a trivial constructor?
I have a (pod) type that needs to be zero-initialized. However, I would also like to create a global instance of this type, and use that global instance from other global constructors. My question is whether it is safe to use the type before it…

user3188445
- 4,062
- 16
- 26
0
votes
2 answers
Static initialization of int array from within structure initialization
I'm trying to statically declare and initialize a structure array containing both char and int arrays.
The following example works well.
typedef struct myStruct
{
char* name;
uint8_t size;
uint8_t *value;
}myStruct;
uint8_t…

Arkaik
- 852
- 2
- 19
- 39
0
votes
0 answers
Is it undefined behaviour for a c++ constant initialized in one .o file to depend on one defined in another file?
I have a C++ project with const string constants in two .o files, where the constant in one file is initialized using the value in the other file. This used to work fine, but now that I'm using a more recent compiler, I'm getting a seg fault as soon…

joanis
- 10,635
- 14
- 30
- 40
0
votes
2 answers
Static final field initialized multiple times
Imagine a java project containing 2 applications. For each of them: a class with a main method to start the app, let's say Main1 and Main2.
Both apps are using a shared Service class containing a final static field :
public class Main1 {
public…

Julien Berthoud
- 721
- 8
- 24
0
votes
1 answer
uncrustify: how to alter the struct initialization braces
I am using uncrustify 0.69.0 (I can't get 0.71.0 to compile yet). I have the following:
static struct {
unsigned char streamid[2];
uint32_t adr;
} plus_crap[3] = { { { 0x00, 0x00 }, 0 },
{ { 0x00, 0x00 }, 0 },
{ { 0x00, 0x00 }, 0 }…

oldunixguy
- 31
- 3
0
votes
2 answers
Creating a value made out of two bytes at compile-time instead of runtime
I have an enumerator which can be used as a uint8_t and I want to use a combination of two values of this enumerator to create a number of type uint16_t where the leftmost and the rightmost bytes are determined by these 2 enumeration values.
I then…

Cristian
- 654
- 1
- 13
- 30
0
votes
1 answer
Flutter: How i implement the list in a list? Error: Only static members can be accessed in initializers.dart(implicit_this_reference_in_initializer)
I want an app with the sellers, buyers and the products of the sellers.
I have a class with the name product, a class with the name seller and a class with the name products provider, in which both classes are written down. I write all the classes…

Tolga
- 335
- 2
- 4
- 17
0
votes
1 answer
Initialization and Assignment of static Variable
#include
int main(void)
{
int i=10;
static int x=i;
if (x == i)
printf("Equal");
else if(x > i)
printf("Greater");
else
printf("Lesser");
return 0;
}
Why this piece of code give compile time error, can someone…

Sanjay Verma
- 101
- 13
0
votes
2 answers
Static string initialization order issue
I have extern const string declaration in a header file, and it's definition in source file:
// a.h
extern const std::string base;
// a.cpp
const std::string base = "base";
Then I have another extern const string in another header file, and the…

user3132457
- 789
- 2
- 11
- 29
0
votes
1 answer
When does dynamic initialization of static volatile variable happen?
First, lets have a look into the standard (C++14 [basic.start.init] 3.6.3/4):
It is implementation-defined whether the dynamic initialization of a non-local variable with static storage duration is done before the first statement of main. If the…

StefanKssmr
- 1,196
- 9
- 16
0
votes
2 answers
Java Initialize List, according to another Size
I want to do something Initialize ArrayList with not null items, depending on its size from another variable.
private static final int SIZE_LIST_GROUP_MAP = 10;
public static final List
user7339033
0
votes
1 answer
How to initialize static const member that is array of another class, and use it?
I am trying to instantiate a vending machine with in inventory. The inventory I planned to be an array of class Drinks. Here is what I have written so far.
VendingMachine.h - should contain array of Drinks class
#include "Drinks.h"
class…

William S
- 167
- 2
- 10