Topic: static vector init
Author: "Johan Hahn" <johahn@delta.telenordia.se>
Date: Thu, 7 Mar 2002 21:02:23 GMT Raw View
Hi
I add the this-pointer to every instance of a class to a static vector in
the same class.
I initialize the static vector in a cpp file with the same name as the class
(just call it class.cpp).
However I also use a global instance of that class in another cpp (lets call
that main.cpp).
The problem is that the global instance in main.cpp is created before the
static vector is initiated in class.cpp. So when I call vector.size() in the
beginning of the main function (in main.cpp) I get 0 even though I *KNOW*
that the pointer was added at some earlier point and has since been cleared
when the vector was initialized.
One would think that all static members of a class would be initialized
before the class can be instanciated but evidently that is not the case.
What also suprises me, is that it works to add the pointer to the vector
even though the vector hasn't yet been initialized.
My question:
Is this the right behaviour of C++ and if so how do I get pass the problem,
or is it just another "feature" in MSVC++ 6.0 SP5.
I found a temporary solution to the problem by initiating the vector in the
same cpp as the global instance (that's main.cpp) just before I create the
global instance, but I can't have it like that much longer.
Thanks beforehand.
...johahn
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "Razvan Cojocaru" <razvanco@gmx.net>
Date: Fri, 8 Mar 2002 16:36:06 GMT Raw View
> One would think that all static members of a class would be initialized
> before the class can be instanciated but evidently that is not the case.
> What also suprises me, is that it works to add the pointer to the vector
> even though the vector hasn't yet been initialized.
The fact that the vector hasn't been initialized doesn't mean that the
memory to be initialized can't be used as a vector. It's just that after you
push_back on that memory, it gets reset when it becomes initialized.
> My question:
> Is this the right behaviour of C++ and if so how do I get pass the
problem,
> or is it just another "feature" in MSVC++ 6.0 SP5.
> I found a temporary solution to the problem by initiating the vector in
the
> same cpp as the global instance (that's main.cpp) just before I create the
> global instance, but I can't have it like that much longer.
As far as I can tell, there's nothing wrong with that behaviour. C++ makes
no guarantees about the order of initialization of global/static variables
across different compilation units.
I can think of two potential solutions. I'm sure there must be more.
1. The obvious one: have a pointer to a vector, initialize that the 1st time
a constructor is called, use the dynamically allocated vector and delete the
vector when the last destructor is called. Although somewhat obvious, this
solution implies a lot of bookkeeping such as how objects of your class
should be copied and initialized from each other, and some sort of reference
counting to figure out when the last object of your class is being
destroyed, so you can safely delete the vector.
2. Take advantage of the fact that although C++ does not guarantee the order
of initialization of variables across compilation units, it _does_ guarantee
that a static variable inside a function is initialized the 1st time the
function is called and it lives roughly until the end of the program. So all
you have to do is write a member function that'll return a reference to a
static vector inside the function.
Here's an example:
// file class.h
#include <vector>
using std::vector;
class Test {
public:
Test() { the_vector().push_back(this); }
static vector<Test *> &the_vector();
};
// file class.cpp
#include "class.h"
vector<Test *>& Test::the_vector()
{
static vector<Test *> v;
return v;
}
// file main.cpp
#include "class.h"
#include <iostream>
using std::cout;
using std::endl;
Test t1, t2, t3, t4;
int main()
{
cout << Test::the_vector().size() << endl;
return 0;
}
There are probably more solutions. I hope this helps.
Regards,
Razvan
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]