# Thread: Order of initialization of static variables

1. ## Order of initialization of static variables

Consider the following code:
Code:
```
int f();

struct A
{
static int a;
}
int A::a = f();

int f()
{
static int i = 0;
return i;
}```
What will A::a be equal to?
Couldn't A::a be initialized before f::i?

The reason I ask is the thread "Virtual Constructors" where my solution depends on the static variables being initialized in a particular order.

2. The rules are a bit obscure, but both A::a and f::i are zero-initialized before anything else. Then A::a is dynamically initialized with the value of f(), which sends execution through f to initialize f::i to the constant expression you give it and returns that value. So A::a will have the same value as f::i. A better example would give f::i a non-default value such as 10.

3. I,m really curious Prelude, where and when you learned that. I have a few books about C++ and never read it anywhere. Do you learn from books + pratice or just write programs?
I must admit that I never thought about some things such as this, if you could give some advices to how to learn it.

4. >I,m really curious Prelude, where and when you learned that.
I have a copy of the C++ standard.

>I have a few books about C++ and never read it anywhere.
What you can learn depends on the books. If they're beginner books then topics of an advanced nature won't be mentioned. For extremely detailed questions about the language, the standard is as good as it gets, but it reads like VCR instructions.

>Do you learn from books + pratice or just write programs?
Well, my library is somewhere around 200 books right now, so I would have to say that I learn by reading and doing, not to mention answering questions here.

>if you could give some advices to how to learn it.
The best way is to read everything you can and apply it. A way to apply what you know as well as get feedback on it is to answer questions here. You'll learn much more quickly if you try to help others to understand something. First, it helps you to solidify your understanding and put it into words. Second, it gives everyone else a chance to double check you and correct you if you're wrong. Third, any test or example code you write will add to your experience. That three-fold method of learning is surprisingly effective.

5. >Well, my library is somewhere around 200 books right now.
Jesus and I though 15 was too many .But I second that on posting here,It has helped me alot.