简体   繁体   中英

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:

#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. 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.)

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.

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 . 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.

If you can use features of the C++11 standard library, you could typedef std::function<size_t (void)> MemSizeFunc; instead of the current typedef. That allows you to store various kinds of functions and function objects that are callable with that signature as a MemSizeFunc . In particular you could register your GetManagedMemSize member function bound to a suitable MemMgrUser object, for example as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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