initializing static members of class

J

John

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
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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

Rolf Magnus

John said:
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.
 
R

Robbie Hatley

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

comp.lang.c++

Not that I know of. Normally, the programmer initializes that,
not the compiler.


Probably because the programmer failed to initialize it.


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;
 
P

prakash.mirji

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.

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<RWCString> to
test whether it is problem with deprecated RWTPtrHashDictionary
template.
But I see that application crashes for RWTValDlist template too.

Please help me.

Thanks,
Prakash
 
A

archimed7592

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<RWCString> to
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.
 
R

Robbie Hatley

Victor Bazarov said:
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
 

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,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top