解法:
/**MyClass.h**/
template<class T> class MyClass{
public:
static int val;
MyClass(int v){ val = v; }
};
/**MyClass.cpp**/
template<typename T>
int MyClass<T>::bar;
template class MyClass < Foo > ;
template class MyClass < Bar > ;
/**main.cpp**/
MyClass<Foo>* foo1 = new MyClass<Foo>(10);
MyClass<Foo>* foo2 = new MyClass<Foo>(15);
MyClass<Bar>* bar1 = new MyClass<Bar>(20);
MyClass<Bar>* bar2 = new MyClass<Bar>(35);
int f1 = foo1->val;//等于15
int f2 = foo2->val; //等于15
int b1 = bar1->val;//等于35
int b2 = bar2->val;//等于35