简体   繁体   English

实现在C ++中向其注册非静态成员函数的工厂

[英]implementing a factory that registers non-static member functions to it in C++

I have a C++ singleton factory-like class called MemMgr which is in charge of managing heap memory for objects in a library: 我有一个名为MemMgr的类似于C ++单例工厂的类,该类负责管理库中对象的堆内存:

#include <vector>
class MemMgr
{
    public:

        // Callback interface of functions to register with MemMgr
        typedef size_t (*MemSizeFunc)(void);

        void Register(MemSizeFunc memSizeFunc);

        static MemMgr & GetInst(void);

        // more public functionality related to managing memory

    private:

        // a vector (not a map) of functions pointers to keep track of
        std::vector<MemSizeFunc> m_memSizeFuncs;

        MemMgr(void);
        MemMgr(MemMgr const &);
        MemMgr & operator= (MemMgr const &);

        // more private functionality related to managing memory
};

What I'd like to be able to do is to have objects of any classes that would like to utilize managed memory be able to register themselves with MemMgr via a ( non-static ) member function which will calculate and return the amount of managed memory that that particular object needs. 我想做的是让任何想要利用托管内存的类的对象能够通过( 非静态 )成员函数向MemMgr注册自己,该成员函数将计算并返回托管内存的数量该特定对象需要的。 Something like the following: 类似于以下内容:

class MemMgrUser
{
    public:

        MemMgrUser(void)
        {
            MemMgr::GetInst().Register(GetManagedMemSize);
        }

    private:

        size_t GetManagedMemSize(void)
        {
            // calculations involving member variables
        }
};

(Then, prior to MemMgr actually allocating any memory, it would query the size-related functions registered to it in order to find out the amount of memory to allocate.) (然后,在MemMgr实际分配任何内存之前,它将查询已注册的与大小相关的函数,以找出要分配的内存量。)

However, the compiler yells at me when I try the above approach b/c I am trying to register member function pointers, not plain-vanilla function pointers. 但是,当我尝试上述方法b / c时,编译器对我大吼大叫,我试图注册成员函数指针,而不是普通函数指针。

Does anyone have any suggestions on how I could implement such functionality? 有人对我如何实现这种功能有任何建议吗? I am having problems seeing how a template implementation (or polymorphic one) would be implemented. 我在查看如何实现模板实现(或多态的实现)时遇到问题。

Thank you, 谢谢,

Aaron 亚伦

You don't even try to register a member function pointer. 您甚至都没有尝试注册成员函数指针。 That would have to be specified as &MemMgrUser::GetManagedMemSize . 必须将其指定为&MemMgrUser::GetManagedMemSize You can't use the plain name of a member function, except in an expression that calls it. 您不能使用成员函数的纯名称,除非在调用它的表达式中使用。

But even if you had a member function pointer, it cannot be used in the same way as a plain function pointer of the same apparent signature. 但是,即使您具有成员函数指针,也无法将其与具有相同外观签名的普通函数指针以相同的方式使用。 Calling a member function always requires an object to call it on. 调用成员函数总是需要一个对象才能对其进行调用。 The this pointer available in the function is an additional, hidden parameter. 函数中可用的this指针是一个附加的隐藏参数。

If you can use features of the C++11 standard library, you could typedef std::function<size_t (void)> MemSizeFunc; 如果可以使用C ++ 11标准库的功能,则可以typedef std::function<size_t (void)> MemSizeFunc; instead of the current typedef. 而不是当前的typedef。 That allows you to store various kinds of functions and function objects that are callable with that signature as a MemSizeFunc . 这使您可以将使用该签名可调用的各种函数和函数对象存储为MemSizeFunc In particular you could register your GetManagedMemSize member function bound to a suitable MemMgrUser object, for example as: 特别是,您可以注册绑定到合适的MemMgrUser对象的GetManagedMemSize成员函数,例如:

MemMgrUser()
{
   MemMgr::GetInst().Register(std::bind(&MemMgrUser::GetManagedMemSize, *this));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM