代码如下
template <class T> class TypeMessage { private: static vector<class Handler*> registry; public: class Handler { public: Handler() { TypeMessage<T>::registerHandler( this ); } virtual void handleEvent(const T* t) = 0; }; void notify() { for(int i = 0; i < registry.size(); i++) registry.at(i)->handleEvent( (T*)this ); } static void registerHandler(Handler* h) { registry.push_back( h ); } };
class On : public TypeMessage<On> { string comment; public: On(string str) { comment = str; } void start() const { cout << "OnEvent.start - " << comment << '\n'; } }; //template<>
vector<TypeMessage<On>::Handler*> TypeMessage<On>::registry;
class Off : public TypeMessage<Off> { string comment; public: Off( string str ) { comment = str; } void stop() const { cout << "OffEvent.stop - " << comment << '\n'; } }; //template<>
vector<TypeMessage<Off>::Handler*> TypeMessage<Off>::registry;
|
定义的两个registry在VC8下编译提示error C2371: 'registry' : redefinition; different basic types 。在g++下编译通过。
经smth上的牛人指点,寻得解决方法,把Class TypeMessage中的private: static vector<class Handler*> registry;放到class Handler的定义之后。名字查找过程中,规则不同。这里的Handler对于VC8编译器来说,和下面的TypeMessage::Handler没有关系,和叫HandlerB一个效果。
而在g++里,把这两者等同了。
阅读(1683) | 评论(0) | 转发(0) |