By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,129 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,129 IT Pros & Developers. It's quick & easy.

static const member initialization

P: n/a
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-lit...html#faq-10.16

Are there other ways?
(Remember: I'm supposed to call srand() only once.)

Regards.
Dec 30 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Spoon" <root@localhostwrote in message
news:45*********************@news.free.fr
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-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);
--
John Carson
Dec 30 '06 #2

P: n/a
Can a function be called in a static member initialization ? I thought
static member initialization should be constants.

Dec 31 '06 #3

P: n/a
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?

Regards.
Dec 31 '06 #4

P: n/a
"subramanian" <su**************@yahoo.comwrote in message
news:11*********************@v33g2000cwv.googlegro ups.com
Can a function be called in a static member initialization ?
Yes.
I thought static member initialization should be constants.
Why?
--
John Carson
Dec 31 '06 #5

P: n/a
"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


Dec 31 '06 #6

P: n/a
John Carson wrote:
subramanian wrote:
>Can a function be called in a static member initialization ?

Yes.
>I thought static member initialization should be constants.

Why?
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
Dec 31 '06 #7

P: n/a
"Spoon" <ro**@127.0.0.1wrote in message
news:45*********************@news.free.fr
John Carson wrote:
>subramanian wrote:
>>Can a function be called in a static member initialization ?

Yes.
>>I thought static member initialization should be constants.

Why?

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.

--
John Carson
Dec 31 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.