c++:成员函数数组

C++:: Array of Member Functions

本文关键字:数组 函数 成员 c++      更新时间:2023-10-16

我想存储不同类的成员函数数组。这里只是重复:

要求:

  • TypeOf
  • 包含函数的类实例
  • AddressOf成员函数
  • 成员函数参数

我可以存储什么:

    包含函数的类实例
  • addressof_成员函数。
  • 成员函数参数

通常不需要存储Class类型,因为可以创建指向Class类型的数组指针。我不能这样做的原因是因为我接收的类类型是未知的和不同的。这个类将被用在许多未知类类型的项目中。

我需要将不同类型的类存储到数组/列表中,在这种情况下,我只是将类的地址存储到数组指针中。

我的问题:当我要调用成员函数时,我需要将类地址强制转换为类类型,但我不知道要强制转换为什么类型。

示例代码(未测试-写得很快):

class A
{
    public:
        void test1(int i);
};
class B
{
    public:
        void test2(char c);
};
class Container
{
    long* objects;
    long* funcs;
    int index;
    public:
        Container()
        {
            objects = new long[5];
            funcs = new long[5];
            index = 0;
        }
        template <class C, typename Types ...>
        void Add(C *obj, void (C::*func)(Types ...))
        {
            objects[index++] = (long)obj;
            funcs[index++] = (long)func;
        }
        typename <Types ...>
        void Call(int inx, Types ... values)
        {
            void (*func)(Types ...) = (void (*)(Types ...))funcs[inx];
            // This is where I've got trouble. I don't store the Class 
            // types, so I don't know what pointer Class type to cast 
            // the Class Instance address to.
            (((*???)objects[inx])->*func)(values ...);
        }
};

提前感谢。

你能在成员函数的签名上稍微约束一下吗?如果是这样,可以存储绑定函数,而不是分别存储指向对象和成员函数的指针。

template<typename... Args>
class Container {
public:
    typedef std::function<void (Args...)> F;
    template <class C>
    void Add(C* obj, void (C::*func)(Args ...))
    {
        functions.push_back( [=](Args... args) {(obj->*func)(args...);} );
    }
    void call(size_t i, Args... args)
    {
        functions[i](args...);
    }
private:
    std::vector<F> functions;
};

IMHO,你的帖子读起来像它可能是一个有趣的多态编程挑战,但增加了要求,"无多态性"…在这种情况下,你要学习更难的方法。

我要解决你所说的问题:

当我要调用成员函数时,我需要强制转换类地址转换为Class类型,但我不知道要转换的类型是什么

过去被称为Thunk(当我第一次遇到它的时候)。在最近的一次搜索中,我没有在这个名字下找到这个想法(我发现了其他几个被称为"thunk"的东西)。我曾经读到的对这个名字的解释是这样的,因为"它概括了我已经思考过的东西"。

请注意,对于一个thunk,不需要强制转换(因为你已经thunk了)

使用thunk作为容器中的对象。

哦,既然你标记了c++,你真的应该使用std::vector .

  // ////////////////////////////////////////////////////////////////////
  // Pure Virtual Thunk_t: an abstract base class
  class PVThunk_t
  {
  public:
     virtual ~PVThunk_t(){}
     virtual void operator()(void* v) = 0;
     virtual void exec      (void* v) = 0;
  };
  // pure-virtual requires derived objects to implement both methods
// ///////////////////////////////////////////////////////////////////////
// template class - make it easy to create thunk for any class T
template <class T>
class Thunk_t : PVThunk_t
{
public:
   // constructor - takes pointer to an object and pointer to a member and stores
   // them in two private variables
   Thunk_t( T*    anInstance,        // pointer to an instance of class T
            void* (T::*aMethod)()) : // pointer to a  method   of class T, no parameter, returns void
      m_instance (anInstance),
      m_method   (aMethod)
      {
         assert  (m_instance);
         asssert (m_method);
      }
   Thunk_t( T*  anInstance,        // pointer to an instance of class T
            T*  (T::*aMethod)()) :  // pointer to a  method   of class T, no parameter, returns T*
      m_instance (anInstance),
      m_method   (aMethod)
      {
         assert  (m_instance);
         asssert (m_method);
      }
   virtual ~Thunk_t() { }
   // override operator "()"
   virtual void* operator()(void* v) { return((*m_instance.*m_method)(v)); }
   // override function "exec"
   virtual void* exec(void* v) { return((*m_instance.*m_method)(v)); }
private:
   T*        m_instance;         // pointer to object of T
   void (T::*m_method)(void*);   // pointer to method attribute of T with void* param
}; // template <class T>  class Thunk_t : public PVThunk_t

注意声明m_instance和m_method指针的正确机制。


如果您需要使用具有可变数量参数的方法,我建议传递一个结构指针(对Thunk进行适当的调整或添加)。单个方法形参总是指向结构体的指针,它的类型或内容可以通过调用的方法推断出来。

我已经很长时间没有使用这个Thunk了,因为,多态性在各个方面都很优越。但是Thunk仍然在编译,所以它可能会工作。

update -注意到虚方法不匹配。固定的