Thread View: comp.lang.c++.moderated
1 messages
1 total messages
Started by Bowden Wise
Mon, 22 Jan 1996 09:52
Static STL containers ? Possible ?
Author: Bowden Wise
Date: Mon, 22 Jan 1996 09:52
Date: Mon, 22 Jan 1996 09:52
78 lines
2757 bytes
2757 bytes
I am creating a singleton class registry. To implement this,
I have created a base singleton class, called Singleton. All of
the other singleton classes are derived from this one, and must
"Register" themselves by calling the base class method:
Singleton::Register (const char* name, Singleton* instance);
The registry is an STL map:
static map<string, Singleton*, less<string> > _registry;
which is a static member of the base class Singleton.
Clearly there is only one instance of each derived singleton. Say
classes A and B are two such derived classes of Singleton. Then I
have tried creating their instances by placing this:
static A instanceA;
static B instanceB;
before main:
void main ()
{
// Lookup singleton A:
Singleton* a = Singleton::Lookup ( "A" );
if ( a )
{
}
}
The static instances of A and B are created BEFORE main() exectues
as expected, however, it aborts during the construction of A.
I get an abort, inside one of the STL tree routines.
Now, if I move the statics *inside* main:
void main ()
{
// statics now in main()
static A instanceA;
static B instanceB;
// Lookup singleton A:
Singleton* a = Singleton::Lookup ( "A" );
if ( a )
{
}
}
then the aborts do not occur.
Can someone explain why this is the case?
I am thinking that since C++ does not gurantee the order of the
construction of statics,
and since the STL relies heavily on static variables, that my statics
must be getting constructed before all of the STL static vars have
been initialized?
Is that possible? What is the solution? Do I have to create
the instances via another class that perhaps 'new''s the single
instances ?
--------------------------------------------------------------------
G. Bowden Wise
Computer Science Dept, Rensselaer Polytechnic Inst, Troy, NY 12180
Email: wiseb@cs.rpi.edu WWW: http://www.cs.rpi.edu/~wiseb/
[ Articles to moderate: mailto:c++-submit@netlab.cs.rpi.edu ]
[ Read the C++ FAQ: http://www.connobj.com/cpp/cppfaq.htm ]
[ Moderation policy: http://www.connobj.com/cpp/guide.htm ]
[ Comments? mailto:c++-request@netlab.cs.rpi.edu ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads