c++11 - Static Class Template member initialization -
i have issue when trying initialize static members of static class template.
basically, thought approach useful for: have lot of objects, of course of same base type have differing object types. want manipulate these objects, that's why decided use static template there quite number of different types these object can consist of.
however, logging , options passing wanted add corresponding members template whithout having write initializers every derived static class.
please note following code not working, because there sdk involved. i'm aksing right approach, not right code.
thanks in advance. :)
template.h:
#ifndef _template_h #define _template_h #include "stats.h" template<class t> class templateobj { public: static void setparameters(const options& options) { t::_options = options; // possible? t::init(); t::dostuff(_options); } protected: static void message() { stats.print("message template static method"); } static stats& templateobj<t>::stats = stats::getinstance(); // not work non-trivial initializer, how correctly? stats::getinstance() retrieves singleton instance static options& templateobj<t>::_options; // possible? }; #endif derived.h:
#ifndef _derived_h #define _derived_h #include "template.h" class derived :templateobj < derived > { public: static void init(); static void dostuff(options& options) }; #endif derived.cpp:
#include "derived.h" void derived::init() { // init stuff here templateobj::message(); // call static method template directly } void derived::dostuff(options& options) { // options stats.print("message derived static method."); // access "stats" here. "stats" should declared , initialized inside template. options.load(); // example } main.h
#include "derived.h" main() { templateobj<derived>::setparameters(new options); }
basically, don't need put templateobj<t>:: before function definition if inside class definition. following 2 both valid:
template<class t> class a{ void func( void ); }; template<class t> void a<t>::func() { /* okay */ } template<class t> class b { void func( void ){ /* okay */ } }; in case, replace following static stats& templateobj<t>::stats = stats::getinstance(); static stats& stat() { return stats::getinstance(); }
and following static options& templateobj<t>::_options; static options& _options;.
on other hand, replace t::_options = options; templateobj<t>::_options = options;.
Comments
Post a Comment