Is it possible to initialize a static member variable after running main?
test.h
class Test
{
static int i;
};
test.cpp
int Test::i = 1;
I know we usually initialize a static member as above. And the static member is initialized before the function runs main
.
My question is:
Is it possible to initialize a static member after being executed by a function main
? Something like that:
int main()
{
// do something
// initialize the Test::i here
}
source to share
No, it cannot be initialized, but there should be no reason why you cannot just call a static setter function that assigns a value i
at runtime. This is the best method.
Or you can alternatively make the constructor always initialize the variable:
#include <iostream>
class Test
{
private:
static int i;
public:
Test()
{
i=2;
}
void print_i (void)
{
std::cout << i << std::endl;
}
};
int Test::i = 1;
int main()
{
Test test;
test.print_i(); // will print 2
}
source to share
I am assuming that you really mean initialization, so that assignment after is main
started. It won't affect basic types like int
or double
, but it can do one for complex data types.
Answer: no, but yes (sort of). There is no way to defer the initialization of a static data member until after it starts main
, but you can use a static member function with a local object object to simulate the effect. This object will be initialized after the function call. In code:
#include <iostream>
struct A {
A() {
std::cout << "A::A()\n";
}
void do_something() {
std::cout << "A::do_something()\n";
}
};
struct B {
static A &a() {
static A instance;
return instance;
}
};
int main() {
std::cout << "main start\n";
B::a().do_something();
B::a().do_something();
std::cout << "main end\n";
}
Will open
main start
A::A()
A::do_something()
A::do_something()
main end
Then you would use B::a()
, which previously would have used B::a
. You will need to make sure the function is not called prior to launch main
.
source to share
The first error is that the variable 'i' is private to the class, so you cannot access it outside of the class. There are two ways to fix this. The first is to make it public (not committed), and the second is to create setter and getter methods.
On the question of the answer, yes, you can. Use the setter method mentioned above. These are two methods:
//…
public:
static int get_i() {
return i;
}
static void set_i(int val) {
i = val;
}
//…
You can call the method like this:
Test::set_i(10);
Note. Remember to include the keyword public
in front of the methods so you can access them outside the class.
source to share
You type word static
for word, and wordconst.
You must initialize const
when you declare it;
You don't have to initialize static
when you declare it, but you can change it anywhere you want, given that you can access it. (it is not, your self is private.)
About thoses instructions, let's look at the C ++ standard ( 3.6.2 ):
Variables with static storage duration or thread storage duration must be initialized to zero before any other initialization.
Technically, yes, your static data is always initialized to zero, even if you explicitly initialize it.
source to share