C++ Static members of a class are initialized after constructor -
i have problem static members of class not being initialized before constructor. doing wrong? bug in g++ ? workarounds?
g++ --version : (ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
using eclipse ide, copy static lib headers /usr/include/statictestlib/inittest.h , library /usr/lib/x86_64-linux-gnu/libstatictestlib.a
note happens if object holds data defined before main , class in static library.
static library header (the static library named statictestlib):
inittest.h#include <iostream> namespace statictestlib { class notifier_header{ public: notifier_header(){ std::cout<<"static var init"<<std::endl; } }; class inittest { public: static notifier_header _header_notifier; inittest(); virtual ~inittest(); }; }
static library source file:
inittest.cpp#include "inittest.h" namespace statictestlib { notifier_header inittest::_header_notifier; class notifier_cpp{ public: notifier_cpp(){ std::cout<<"code before constructor"<<std::endl; } }_notifier_in_cpp; inittest::inittest() { std::cout<<"constructor"<<std::endl; } inittest::~inittest() { std::cout<<"destructor"<<std::endl; } }
this program:
statictest.cpp#include <iostream> #include <statictestlib/inittest.h> statictestlib::inittest test; int main() { std::cout << "program main" << std::endl; std::cout << "program end" << std::endl; return 0; }
… outputs:
constructor static var init code before constructor program main program end destructor
but program:
#include <iostream> #include <statictestlib/inittest.h> int main() { std::cout << "program main" << std::endl; statictestlib::inittest test; std::cout << "program end" << std::endl; return 0; }
… outputs:
static var init code before constructor program main contructor program end destructor
my guess related order of static objects initialisation in different compilation units being undefined.
the second code snippet create test
object in main
easy explain. static initialisation happen before code executed, time enter main, notifier_header
object created.
now, when create test
before main
, have 2 static objects. notifier_header
object not depend on inittest
: scoped within class, stored in static memory. seem reference notifier_header
in inittest.cpp
, different compilation unit main
. compiler free static allocations in order 2 units, provided there no interdependencies.
if constructor depended on notifier_header
, use singleton. create function returns instance of static object (headerinstance
in example below), , upon call, object created:
#include <iostream> namespace statictestlib { class notifier_header{ public: notifier_header(){ std::cout<<"static var init"<<std::endl; } }; class inittest { public: inittest(); virtual ~inittest(); notifier_header& headerinstance(); }; }
static library source file (inittest.cpp)
#include "inittest.h" namespace statictestlib { class notifier_cpp{ public: notifier_cpp(){ std::cout<<"code before constructor"<<std::endl; } }_notifier_in_cpp; inittest::inittest() { headerinstance(); std::cout<<"constructor"<<std::endl; } inittest::~inittest() { std::cout<<"destructor"<<std::endl; } notifier_header& inittest::headerinstance() { static notifier_header _header_notifier; // constructed once return _header_notifier; } }
the output get:
static var init constructor code before constructor program main program end destructor
Comments
Post a Comment