i have here singleton class, called master
. class has other entities specific job. master
class has get
method these entities.
namespace framework { class master { private: networkcontroller * mnetworkcontroller; filecontroller * mfilecontroller; static master * ginstance; public: static master * getinstance(); networkcontroller * getnetworkcontroller(); filecontroller * getfilecontroller(); } }
i use these entities allthoughout program so:
framework::master::getinstance() -> getnetworkcontroller() framework::master::getinstance() -> getfilecontroller()
as can see, it's bit long , time-consuming type in. tried putting them in macro, so:
#define network_controller() framework::master::getinstance() -> getnetworkcontroller() #define file_controller() framework::master::getinstance() -> getfilecontroller() namespace framework { class master { ... } }
however, find method messy , confusing. tried inline
methods , added them in 1 header file.
#ifndef __frameworkhelper__ #define __frameworkhelper__ inline framework::networkcontroller * masternetworkcontroller() { return framework::master::getinstance() -> getnetworkcontroller(); } inline framework::filecontroller * masterfilecontroller() { return framework::master::getinstance() -> getfilecontroller(); } #endif
but gave me compile errors when trying use them on different class saying:
error c2653: 'framework' : not class or namespace name
i don't know why throw error this. how can make these inline methods available allthroughout program?
there no reference of namespace framework
in header file, can fix doing :
#ifndef __frameworkhelper__ #define __frameworkhelper__ #include "framework.h" // framework defined inline framework::networkcontroller * masternetworkcontroller() { return framework::master::getinstance() -> getnetworkcontroller(); } inline framework::filecontroller * masterfilecontroller() { return framework::master::getinstance() -> getfilecontroller(); } #endif
or in framework header :
#ifndef __framework__ #define __framework__ namespace framework { class master { ... } } inline framework::networkcontroller * masternetworkcontroller() { return framework::master::getinstance() -> getnetworkcontroller(); } inline framework::filecontroller * masterfilecontroller() { return framework::master::getinstance() -> getfilecontroller(); } #endif
but prefer first solution....
another solution, instead of using inline
methods set getnetworkcontroller
, getfilecontroller
static
, use them :
master::getnetworkcontroller(); master::getfilecontroller();
in own opinion easier understand.
Comments
Post a Comment