"Spoon" <ro**@127.0.0.1wrote in message
news:45*********************@news.free.fr
John Carson wrote:
>Spoon wrote:
>>I have a Packet class I use to send packets over the Internet. All
the packets sent in a session are supposed to share a common random
ID. I figured I'd use a static const member inside my class.
class Packet
{
static const int session_id;
...
};
My problem is: where (and when) do I initialize session_id.
On my first try, I added the following statement in packet.cpp
const int Packet::session_id = rand();
But, as far as I understand, this initialization occurs before
main() starts, thus, before I can call srand().
Therefore, this implementation seems incorrect.
I suppose I can use the so-called "construct-on-first-use" idiom
from the C++ FAQ.
http://www.parashift.com/c++-faq-lit...html#faq-10.16
Are there other ways?
How about:
class Packet
{
static const int session_id;
};
int InitializeStatic(unsigned seed)
{
srand(seed);
return rand();
}
const int Packet::session_id = InitializeStatic(5);
Hello John,
The problem with this solution is that I have other classes that call
rand(). I don't want to make class Packet responsible for initializing
the RNG. For example, if I don't use Packet in one of my programs, and
if I also use the other classes, the RNG will always be seeded with 0.
The solution I'm looking for is:
call srand in main
all static const members are initialized after that point.
Does someone know how to do that?
It can't be done.
A version of "construct-on-first-use", as you previously suggested, would
seem to be your best bet, e.g.,
class Packet
{
static const int session_id;
};
int InitializedRand(unsigned seed=0)
{
static bool needInitialization = true;
if(needInitialization)
{
srand(seed);
needInitialization = false;
}
return rand();
}
const int Packet::session_id = InitializedRand(5);
In use, you would call InitializedRand in place of your first call to rand()
for the initialization of *each* static variable that requires it.
In case there aren't any static variables requiring rand(), you would also
use it in place of your first call to rand() from main() (which is most
simply accomplished by always calling it at the start of main() ). As an
alternative to this call from main, you might choose to have an object that
is a static variable in every program that you write --- an "application"
object --- and call the function in its initialization.
--
John Carson