Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static const member initialization

Reply
Thread Tools

static const member initialization

 
 
Spoon
Guest
Posts: n/a
 
      12-30-2006
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.
 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      12-30-2006
"Spoon" <root@localhost> wrote in message
news:45968e9b$0$307$(E-Mail Removed)
> 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


 
Reply With Quote
 
 
 
 
subramanian
Guest
Posts: n/a
 
      12-31-2006
Can a function be called in a static member initialization ? I thought
static member initialization should be constants.

 
Reply With Quote
 
Spoon
Guest
Posts: n/a
 
      12-31-2006
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.
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      12-31-2006
"subramanian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com
> Can a function be called in a static member initialization ?


Yes.

> I thought static member initialization should be constants.


Why?


--
John Carson


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      12-31-2006
"Spoon" <root@127.0.0.1> wrote in message
news:45977bea$0$323$(E-Mail Removed)
> 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




 
Reply With Quote
 
Spoon
Guest
Posts: n/a
 
      12-31-2006
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
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      12-31-2006
"Spoon" <root@127.0.0.1> wrote in message
news:4597be65$0$296$(E-Mail Removed)
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
initialization of a const static float data member in a class akomiakov@gmail.com C++ 23 06-15-2008 07:58 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
const static Vs. static const Dave C++ 10 05-22-2005 10:32 PM
About static const members appearing in another static const definitions Rakesh Sinha C++ 4 01-13-2005 08:11 AM



Advertisments