且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

函数指向模板类成员函数的指针

更新时间:2022-03-08 09:38:30

确定,所以函子解决方案不能按照需要工作。也许你应该让你的模板类继承自一个通用的基础接口类。然后你使用那​​些的向量。

Ok, so the functor solution doesn't work as you need. Perhaps you should have your template class inherit from a common base "Interface" class. And then you use a vector of those.

这样的东西:

class Base { 
public:
  virtual ~Base(){}
  virtual void DoSomething() = 0;
}

template <class T> class MyClass : public Base {
public:
    void DoSomething(){}
};

std::vector<Base *> objects;
objects.push_back(new MyClass<int>);
objects.push_back(new MyClass<char>);