S
Spoon
Hello everyone,
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-lite/ctors.html#faq-10.16
Are there other ways?
(Remember: I'm supposed to call srand() only once.)
Regards.
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-lite/ctors.html#faq-10.16
Are there other ways?
(Remember: I'm supposed to call srand() only once.)
Regards.