使用wizard生成.h及.cpp
哎,C++的入门啊,搞了3个小时
洗洗,睡了
- #include <string>
- #include <map>
- using namespace std;
- class Tool
- {
- protected:
- Tool();
- public:
- static Tool* getInstance();
- public:
- string getDescription(short id);
- private:
- static map<short,string> dictionary;
- private:
- static Tool* instance ;
- };
- #include "stdafx.h"
- #include "CommonUtils.h"
- #include <map>
- #include <string>
- //#include <utility>
- using namespace std;
- map<short,string> Tool::dictionary;//定义map对象
- Tool* Tool::instance=0;//对.h中的静态参量进行赋值
- Tool::Tool()
- {
- //使用pair向map中填充<key,value>值对
- dictionary.insert(pair<short,string>(1,"value"));
- }
- Tool* Tool::getInstance(){
- if (0==instance)
- {
- instance = new Tool;
- }
- return instance;
- }
- string Tool::getDescription(short id){
- //必须使用<short,string>进行指定模板参数,才能使用后续的second
- std::map<short,string>::iterator it =dictionry.find(id);
- return (*it).second;
- }
洗洗,睡了