472,789 Members | 1,414 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

static const member initialization

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
7 3937
"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
Can a function be called in a static member initialization ? I thought
static member initialization should be constants.

Dec 31 '06 #3
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
"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
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
5
by: Minti | last post by:
How do we initialize static const data in C++, I tried class Foo { static const std::string name = "MyName"; }; I don't see any reason as to why this must not work.
2
by: utab | last post by:
Dear all, I am confused at some point on the initialization of static map member of a class. The class I have designed id something like this. class Class_name{ public: private:
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
10
by: n.torrey.pines | last post by:
Are global variables (and const's) guaranteed to be initialized before static class members (and methods) ? const int x = 19907; int get_x() { return x; } // another compilation unit: ...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
2
by: Ranganath | last post by:
Hi, Why is there a restriction that only integral types can be made static constant members of a class? For e.g., class B { private: static const double K = 10; };
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.