Looks fine to me, but for the fact that descrToFuncMap
needs to be declared static
if you intend to initialise it from inside the static function Initialize()
.
If you want to make sure that Initialize()
gets called, and gets called just once, you can use the Singleton pattern. Basically, if you aren't doing multithreading, that just means wrapping descrToFuncMap
inside its own class (called say FuncMap
) with a private constructor that calls Initialize()
. Then you add a static
local variable of type FuncMap
to Processor::Process()
-- because the variable is static
, it persists and is only initialised once.
Example code (I now realise that friend
isn't really necessary here):
class Processor {
private:
typedef double (MyClass::*MemFuncGetter)();
class FuncMap {
public:
FuncMap() {
descrToFuncMap["X"]=&MyClass::GetX;
descrToFuncMap["SomethingElse"]=&MyClass::GetSomethingElse;
descrToFuncMap["RR"]=&MyClass::GetRR;
descrToFuncMap["T"]=&MyClass::GetT;
}
// Of course you could encapsulate this, but its hardly worth
// the bother since the whole class is private anyway.
map<std::string, MemFuncGetter> descrToFuncMap;
};
public:
void Process(Myclass m, string);
};
void Processor::Process(MyClass ms, const std::string& key) {
static FuncMap fm; // Only gets initialised on first call
map<std::string, Getter>::iterator found=fm.descrToFuncMap.find(key);
if(found!=fm.descrToFuncMap.end()) {
MemFuncGetter memFunc=found->second;
double dResult=(ms).*memFunc();
std::cout<<"Command="<<key<<", and result="<<result<<std::end;
}
}
This is not the "true" Singleton pattern as different functions could create their own, separate instances of FuncMap
, but it's enough for what you need. For "true" Singleton, you would declare FuncMap
's constructor private and add a static method, say getInstance()
, which defined the one-and-only instance as a static
variable and returned a reference to that. Processor::Process()
would then use this with
FuncMap& fm = FuncMap::getInstance();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…