I need to create a vector which holds the pointers to some static objects (e.g., a1, a2, a3).
void foo()
{
static TEST a1, a2, a3;
static vector<TEST *> m_test;
m_test.push_back( &a1 );
m_test.push_back( &a2 );
m_test.push_back( &a3 );
}
But I don’t want to have to write out the declaration for each static object.
How can I use a loop to do this? I have tried something like:
for (unsigned int i=0; i<10; i++)
{
m_test.push_back( &(static TEST()));
}
But it seems the static doesn’t have any effect inside an expression, as the objects get destroyed, as I’d expect from a normal temporary.
Function-static objects may only be created by a defining declaration with a name. You can’t create a “function-
statictemporary” like that.How about:
Otherwise, you have no option but to resort to dynamic-storage objects, the only objects whose very existence in any given scope can be controlled programmatically (not least because they’re not really in any scope at all). In fact, this is pretty much the use case for objects of dynamic storage duration.