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

initializing static members of class

P: n/a
Hello,

is there any compiler option for g++ for initializing static members of the
class.
Due to some unknown reason, static member in one of our c++ application is
not getting initialized properly.

Please help me on this.

Thanks,
Prakash
May 28 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 28 Maj, 16:25, "John" <prakash.mi...@gmail.comwrote:
Hello,

is there any compiler option for g++ for initializing static members of the
class.
Due to some unknown reason, static member in one of our c++ application is
not getting initialized properly.
Most probable reason for this is that you do not initialize it in the
code, but you have not shown us any code so we can't tell.

--
Erik Wikstr鰉

May 28 '07 #2

P: n/a
John wrote:
Hello,

is there any compiler option for g++ for initializing static members of
the class.
Due to some unknown reason, static member in one of our c++ application
is not getting initialized properly.
You mean the version you use has a bug that ignores your initialization
sometimes? In that case, I doubt that it has a compiler switch to turn that
off.

May 28 '07 #3

P: n/a

"John" <pr***********@gmail.comwrote:
is there any compiler option for g++ for initializing static
members of the class?
Not that I know of. Normally, the programmer initializes that,
not the compiler.
For some unknown reason, static member in one of our c++
application is not getting initialized properly.
Probably because the programmer failed to initialize it.
Please help me on this.
Ok.

Initialize the static members in the source code (not the
header!) for the module.

Example: I have a module called blat1, with header blat1.h
and source blat1.cpp. This module has a class called dumbo
with a static member called asdf. Then, initialize asdf in
blat.cpp, like so:

// in blat1.h:
class dumbo
{
public:
static int asdf;
}

// in blat1.cpp, at global scope:
dumbo::asdf (42);
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lonewolf aatt well dott com
triple-dubya dott tustinfreezone dott org
May 28 '07 #4

P: n/a
On 5月28日, 下午11时47分, "Robbie Hatley" <bogus.addr...@no.spamwrote:
"John" <prakash.mi...@gmail.comwrote:
is there any compiler option for g++ for initializing static
members of the class?

Not that I know of. Normally, the programmer initializes that,
not the compiler.
For some unknown reason, static member in one of our c++
application is not getting initialized properly.

Probably because the programmer failed to initialize it.
Please help me on this.

Ok.

Initialize the static members in the source code (not the
header!) for the module.

Example: I have a module called blat1, with header blat1.h
and source blat1.cpp. This module has a class called dumbo
with a static member called asdf. Then, initialize asdf in
blat.cpp, like so:

// in blat1.h:
class dumbo
{
public:
static int asdf;

}

// in blat1.cpp, at global scope:
dumbo::asdf (42);
maybe this works
int dumbo::asdf=42;

May 28 '07 #5

P: n/a
comp.lang.c++ wrote:
On 5月28日, 下午11时47分, "Robbie Hatley" <bogus.addr...@no.spamwrote:
>"John" <prakash.mi...@gmail.comwrote:
>>is there any compiler option for g++ for initializing static
members of the class?

Not that I know of. Normally, the programmer initializes that,
not the compiler.
>>For some unknown reason, static member in one of our c++
application is not getting initialized properly.

Probably because the programmer failed to initialize it.
>>Please help me on this.

Ok.

Initialize the static members in the source code (not the
header!) for the module.

Example: I have a module called blat1, with header blat1.h
and source blat1.cpp. This module has a class called dumbo
with a static member called asdf. Then, initialize asdf in
blat.cpp, like so:

// in blat1.h:
class dumbo
{
public:
static int asdf;

}

// in blat1.cpp, at global scope:
dumbo::asdf (42);
maybe this works
int dumbo::asdf=42;
Or

int dumbo::asdf(42);

(that's what Robbie probably meant).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 28 '07 #6

P: n/a
On May 28, 7:30 pm, Erik Wikstr鰉 <eri...@student.chalmers.sewrote:
On 28 Maj, 16:25, "John" <prakash.mi...@gmail.comwrote:
Hello,
is there any compiler option for g++ for initializing static members ofthe
class.
Due to some unknown reason, static member in one of our c++ application is
not getting initialized properly.

Most probable reason for this is that you do not initialize it in the
code, but you have not shown us any code so we can't tell.

--
Erik Wikstr鰉
FYI

Here is the code:

Application is using below mentioned template class which is declared
as static in RuleEngine.h class file.

static RWTPtrHashDictionary<RWCString, RuleClass
RWDefHArgs(RWCString)>ruleHash;
RWTPtrHashDictionary<RWCString, RuleClass RWDefHArgs(RWCString)>
RuleEngine::ruleHash(RWCString::hash);

Above line definition/initialization for ruleHash is found in
RuleEngine.C at the beginning of the file.

Here is sample piece of code where ruleHash is used. This code is
RuleEngine.C. I am calling insertRule(this) static method from
RuleClass.h class file.

Void RuleEngine::insertRule( RuleClass* newRule )
{
RuleClass* rule = NULL;
if( 0 != ruleHash.entries() )
............
Core dump....

}

Application is using template object ruleHash to call entries()
method.
I am getting core dump at releHash.entries() line. Its not only with
entries method, it crashes for any methods of RWTPtrHashDictionary.
I have also tried with other template class RWTValDlist<RWCStringto
test whether it is problem with deprecated RWTPtrHashDictionary
template.
But I see that application crashes for RWTValDlist template too.

Please help me.

Thanks,
Prakash

May 28 '07 #7

P: n/a
On May 28, 9:29 pm, prakash.mi...@gmail.com wrote:
On May 28, 7:30 pm, Erik Wikstr鰉 <eri...@student.chalmers.sewrote:
On 28 Maj, 16:25, "John" <prakash.mi...@gmail.comwrote:
Hello,
is there any compiler option for g++ for initializing static members of the
class.
Due to some unknown reason, static member in one of our c++ application is
not getting initialized properly.
Most probable reason for this is that you do not initialize it in the
code, but you have not shown us any code so we can't tell.
--
Erik Wikstr鰉

FYI

Here is the code:

Application is using below mentioned template class which is declared
as static in RuleEngine.h class file.

static RWTPtrHashDictionary<RWCString, RuleClass
RWDefHArgs(RWCString)>ruleHash;

RWTPtrHashDictionary<RWCString, RuleClass RWDefHArgs(RWCString)>
RuleEngine::ruleHash(RWCString::hash);

Above line definition/initialization for ruleHash is found in
RuleEngine.C at the beginning of the file.

Here is sample piece of code where ruleHash is used. This code is
RuleEngine.C. I am calling insertRule(this) static method from
RuleClass.h class file.

Void RuleEngine::insertRule( RuleClass* newRule )
{
RuleClass* rule = NULL;
if( 0 != ruleHash.entries() )
............
Core dump....

}

Application is using template object ruleHash to call entries()
method.
I am getting core dump at releHash.entries() line. Its not only with
entries method, it crashes for any methods of RWTPtrHashDictionary.
I have also tried with other template class RWTValDlist<RWCStringto
test whether it is problem with deprecated RWTPtrHashDictionary
template.
But I see that application crashes for RWTValDlist template too.

Please help me.

Thanks,
Prakash
It seems RWCString::hash is constructed after the
RuleEngine::ruleHash(and result is unpredictable)...
Use Singleton pattern to fix that.

May 28 '07 #8

P: n/a

"Victor Bazarov" <v.********@comAcast.netwrote in message news:mp******************************@comcast.com. ..
int dumbo::asdf(42);

(that's what Robbie probably meant).
Ah, yes... good catch. For some reason I tend to forget
to include the type declarator when defining static class
members. My compiler keeps yelling at me about that. :-)

--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lonewolf aatt well dott com
triple-dubya dott tustinfreezone dott org
Jun 2 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.