Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C++ Programming

initializing static members of class


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

On 28 Maj, 16:25, "John" <prakash.mi@gmail.com> 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.

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 Wikstrm

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.

"John" <prakash.mi@gmail.com> wrote:
> 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

On 5 28 , 11 47 , "Robbie Hatley" <bogus.addr@no.spam> wrote:

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

On May 28, 7:30 pm, Erik Wikstrm <eri@student.chalmers.se> wrote:

> On 28 Maj, 16:25, "John" <prakash.mi@gmail.com> 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.

> 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 Wikstrm

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

On May 28, 9:29 pm, prakash.mi@gmail.com wrote:

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

"Victor Bazarov" <v.Abaza@comAcast.net> wrote in message news:mpednXHQJOh_n8bbnZ2dnUVZ_hGdnZ2d@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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc