static const member initialization

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.
 
J

John Carson

Spoon said:
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?

How about:

class Packet
{
static const int session_id;
};

int InitializeStatic(unsigned seed)
{
srand(seed);
return rand();
}

const int Packet::session_id = InitializeStatic(5);
 
S

subramanian

Can a function be called in a static member initialization ? I thought
static member initialization should be constants.
 
S

Spoon

John said:
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?

Regards.
 
J

John Carson

Spoon said:
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.
 
J

John Carson

Spoon said:
In C, AFAIU, objects with static duration must be initialized with
static initializers, i.e. expressions that the translator can evaluate
prior to program startup.

http://www-ccs.ucsd.edu/c/declare.html


It makes sense that C++ would not follow C in this respect. Objects of class
type all have constructors to initialize them. Thus if any object of class
type is static, it is initialized by a function (i.e., the constructor) even
where no function is explicitly called.

It would be very inconvenient if the permitted form of initialization for
class objects differed depending on whether they were static variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top