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 466461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:28:57+00:00 2026-05-12T23:28:57+00:00

My previous question about this subject was answered and I got some tests working

  • 0

My previous question about this subject was answered and I got some tests working nice.
Map functions of a class

My question is now, if there is a way to while declaring the function, be able to register it in a map, like I realized in this question about namespaces and classes:
Somehow register my classes in a list

the namespaces and classes was fine to register in a map using the “static” keyword, with that, those static instances would be constructed before the main() be called.

Can I do that somehow with class functions?
because when I use static keyword inside a class declaration, I can’t initialize the member as I can outside the class declaration(as with namespaces and classes in the second url above)

I guess I could hardcode all members inside the constructor and register them in a map, but I would like to know if there is a way to do that while I declare the members, to make it easier in the future

Thank you,
Joe

  • 1 1 Answer
  • 1 View
  • 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-05-12T23:28:57+00:00Added an answer on May 12, 2026 at 11:28 pm

    What is your problem here ?

    The problem is that, unfortunately, in C++ functions are not considered first class members.

    Oh sure there are those pointers to functions that work pretty well, but there is no generic function type or anything like that.

    There are however ways to work around this, the simplest I think being the Command pattern.

    In the Command pattern a function (operation) is abstracted away in an object. The arguments are stored in the object for later reuse (for example undo or redo command) and a unified interface exists to perform the operation itself.

    Less talk, more code:

    class Command
    {
    public:
      virtual ~Command() {}
    
      virtual Command* clone() const = 0;
    
      virtual void execute() = 0;
    };
    

    Simple ?

    class Foo {};
    
    class FooCommand: public Command
    {
    public:
      void parameters(Foo& self, int a, std::string const& b);
    
      virtual FooCommand* clone() const;
      virtual void execute();
    
    private:
      Foo* m_self;
      int m_a;
      std::string const* m_b;
    };
    

    Now, the sweet thing is that I can simply store my command in a map.

     // registration
     typedef boost::ptr_map<std::string, Command> commands_type;
    
     commands_type commands;
     commands.insert("foo", FooCommand());
    
     // get the command
     Foo foo;
     FooCommand* cFoo = dynamic_cast<FooCommand*>(commands["foo"].clone());
     if (cFoo != 0)
     {
       cFoo->parameters(foo, 2, "bar");
       cFoo->execute();
     }
    

    This proposal would still require some work.

    • passing the parameters is quite annoying since it requires a down cast.
    • I did not concern myself with exception safety, but returning an auto_ptr or a shared_ptr would be better for the clone method…
    • the distinction between a const and non-const Foo argument is not that easy to introduce.

    However it is safer than using a void* to store the pointers to function in you map since you have the advantage of RTTI to check whether or not the type is correct.

    On the other hand, printing the collection of Commands linked to a particular object is incredibly easy now (if you have one map per object), you can also find ways to emulate the effect of virtual methods etc…

    But I hope you realize that you are in fact trying to implement reflection, and it’s not gonna be easy… good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a previous question about overriding Time.now , I was working toward a solution
Surprisingly I was only able to find one previous question on SO about this
This question is a follow up to my previous question about getting the HTML
This is a follow-up to a previous question I had about interfaces. I received
this question is an extension to a previous question i asked (and was answered).
This is a follow up post of my previous question about BASIC auth over
This follows on from my previous question about Moose structured types. I apologise for
In a previous question, I asked about various ORM libraries. It turns out Kohana
In a previous Git question , Daniel Benamy was talking about a workflow in
This question is directly related to my previous question ASP.NET AJAX Is it possible

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.