c++ - Static variable not initialized -
i've got strange problem static variable not initialized should be.
i have huge project runs windows , linux. linux developer doesn't have problem suggest kind of wired visual studio stuff.
header file
class myclass { // other stuff here ... private: static anotherclass* const default_; };
cpp file
anotherclass* const myclass::default_(new anotherclass("")); myclass(anotherclass* const var) { assert(default_); ... }
problem default_
is null
. tried breakpoint @ initialization of variable cannot catch it.
there similar problem in class.
cpp file
std::string const myclass::mystring_ ("sometext"); myclass::myclass() { assert(mystring_ != ""); ... }
in case mystring_
is empty. again not initialized.
does have idea that? visual studio settings problem?
cheers simon
edit:
i came across static initialization fiasco. i'm not sure if problem because there no problems linux compiler. shouldn't compiler react same way in case?
i suggest use static member function static variable , not static variable itself:
class myclass { // other stuff here ... private: static anotherclass* const getanotherclass(); }; anotherclass *const myclass::getanotherclass() { static anotherclass *const p = new anotherclass(""); return(p); }
the standard guarantees p initialized once when function called first time, initialized object (unless you've exhausted memory or constructor threw).
please note - may or may not thread safe (depends on compiler really).
and yet note - have live "memory leak" next impossible decide when destroy object , have no way reset p null.
Comments
Post a Comment