Function pointer to member function of a template class

I have a template class defined (partially) as

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

If I want to call DoSomething from another class, but I can do it for several "T" types in the same place, I am stuck with the idea, since the function pointers of the method are uniquely bound to the class type. Of course, each MyClass is a different type, so I cannot store pointers to objects in MyClassDoSomething () in a "polymorphic" form.

My use case is that I want to save the vector of function pointers to “DoSomething” in the hold class so that I can call the call for all saved classes from one place.

Does anyone have any suggestions?

+3
source share
2 answers

, functor , . , "". .

- :

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>);
+12

, , . Bizzarly , , , . , , (.. ).

!

0

Source: https://habr.com/ru/post/1697627/


All Articles