Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9285885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T19:16:43+00:00 2026-06-18T19:16:43+00:00

I have a C++ singleton factory-like class called MemMgr which is in charge of

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T19:16:44+00:00Added an answer on June 18, 2026 at 7:16 pm

    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));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a singleton implemented like this: class Test123(object): _instance = None def __new__(cls,
I have a Singleton which has an NSMutableArray containing a Class, the Class contains
I have a singleton class which is being used throughout the app. I am
I have a class that would normally just generate factory objects, however this class
I have a normal java bean which is singleton with jms resource like this
I have a singleton factory and would like it to return a reference to
Say I have a singleton-ish, factory-ish, reflection-ish class that receives some input, and spits
Say I have a class called PermissionManager which should only exist once for my
I am developing a swing app, where I have a Factory class which provide
I have a singleton class for global access to config information. This singleton class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.