Re: Question about a static vector<*A> in class A
On Jul 25, 1:56 pm, Jojo <no_m...@thanx.com> wrote:
Ondra Holub schreef:
But you cannot test on 'vector_ptr' because it may not be initialized,
just like the code I had originally...
Static data (which is not defined in any function) is automatically
initialized to 0, so you can test it for 0 and initialize it. This is
valid only for static data defined in any class/struct or in global
scope.
[...]
Sorry, but I'm lost here... In my original post I coded something like:
class A {
static std::vector<A> my_vector;
// rest of class omitted
};
And you replied:
"If you place your static data inside a class as private data, you
still have no guaranty, that it will be initialized before usage,
because any public method of your class may be called during
initialization of other static stuff."
So I can understand the solution to put the static vector in a method of
class A (like I tried to give an example for in my last post). Or was
that attempt of me not correct?
In your example you have:
class A {
static A* instance; // Vector or just A* does not matter here....
// rest of class omitted
};
and suddenly it is OK to use the value of 'instance' in a public method
("if (instance == 0)" in "GetInstance()"). I don't see differences in
the two pieces of code regarding to the possible crash...
std::vector requires dynamic initialization, that the
constructor be run. A pointer doesn't.
If you rely on the fact that 'instance' is initialized when
you first use it in a public method, why can't you rely on the
same initialization for 'my_vector'?
Because there are three different types of initialization (for
objects with static lifetime): zero initialization, static
initialization, and dynamic initialization. The are guaranteed
to take place in that order; zero initialization and static
initialization are guaranteed to take place before a single line
of executable code that you wrote is executed.
If the pointer has no initialization expression, it is zero
initialized, and if the initialization expression is a constant
expression, it is static initialized. In both cases,
initialization is guaranteed to occur before any of the code you
wrote is run.
In a single threaded environment, the easiest way to implement
what you need is to use a simple static function:
std::vector< A >&
A::ourVector()
{
static std::vector< A > theOneAndOnly ;
return theOneAndOnly ;
}
Note that this may leave you with an order of destruction
problem, however. To avoid the order of destruction problem as
well, you can use:
std::vector< A >&
A::ourVector()
{
static std::vector< A >* theOneAndOnly ;
if ( theOneAndOnly == NULL ) {
theOneAndOnly = new std::vector< A > ;
}
return *theOneAndOnly ;
}
This guarantees that the vector will never be destructed.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34