> As is typical, after I posted this question I figured out what is wrong.
>
[quoted text clipped - 3 lines]
>
> That is, it's all in the timimg! :)
Thanx Ben!
I had heard of, but never really paid attention to, a 'static constructor'.
I guess you learn something new everyday... :)
Prompted by your post, I looked 'static constructor' up. I was interested in
how it differed from a 'normal constructor'. I discovered that it is a
constructor that is called only once, and used to intiailize static members.
I believe it is called no matter which actual contructor is used to create
the first instance, and the static constructor is called first. Then again,
the static constructor might be called at application start up, which would
be before ANY instances are created.
What are the exact rules for when a static constructor is called? Is it
called at applicaton start up? At creation of the first instance? At the
creation of an instance when there are currently no instances (this goes to
what happens to the static members if the last existing instance is
destroyed)?
[==P==]
>> As is typical, after I posted this question I figured out what is wrong.
>>
[quoted text clipped - 95 lines]
>>>>
>>>> //-------------------------------------------------------------
Ben Voigt [C++ MVP] - 24 Sep 2007 23:17 GMT
> Thanx Ben!
>
[quoted text clipped - 14 lines]
> to what happens to the static members if the last existing instance is
> destroyed)?
The rule is "no later than the first time any member of the type is used".
In reality, it is at that exact point, just before the runtime loads and
JITs the type the first time. Often, this will be the first time you call a
constructor, but accessing a static member would also cause the type
initializer to run.
But the standard appears ambiguous enough to allow it to run when the
application starts, or when the assembly is first loaded, etc.