Hi all!
I have a small problem I cannot find a good solution. The problem is
destructing global (static) variables in correct order. I try to mimic it in
here.
// file1.cpp contains implementation of Class1
static Class1 instance;
// static
void* Class1::alloc()
{
return ::instance.allocresource();
}
// static
void Class1::free( void* resource )
{
::instance.freeresource( resource );
}
--------------------------------------
// file2.cpp contains implementation of Class2
void Class2::foo()
{
this->Add( Class1::alloc() );
}
Class2::~Class2()
{
this->Clear(); // this calls Class1::free( resource ) in a loop
}
--------------------------------------
// file3.cpp
// this is an user file that uses Class2
static Class2 resources;
--------------------------------------
So, Class2 uses Class1 and both classes (instances) store data into their
own variables. Everything would be perfect if Class2 "resources" would be
destructed before Class1 "instance". Compiler just decides to destruct
"instance" first. It causes a problem because destructor of "resources"
needs "instance" (of course it has to tell to "instance" that it does not
need resources anymore).
More background: Class2 is used generally and it should be possible to be
global variable. Class1 is a global resource that Class2 needs. It is just
for implementation (user of Class2 should not be aware of it).
How would you solve the problem?
Thanks in advance,
JMu