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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:18:00+00:00 2026-06-07T06:18:00+00:00

I think that any explanation without the code would be just more obscure. So

  • 0

I think that any explanation without the code would be just more obscure. So here is the code where I tried to keep everything as simple as possible.

#include <vector>
#include <iostream>

class WithParametersBase
{
public:
    WithParametersBase();

    double getX() const {return 0.0;}
    double getY() const {return 1.0;}

    //let's say I want to access these members using an unified interface:

    double getParameter(int index) const;

    // For example index == 0 means getX and index == 1 means getY.
    // I could implement it for example like this:

protected:

    void addGetter(double (WithParametersBase::* getter)()const)
    {
        getters_.push_back(getter);
    }

    std::vector<double (WithParametersBase::*)()const> getters_;
};

WithParametersBase::WithParametersBase()
{
    addGetter(&WithParametersBase::getX);
    addGetter(&WithParametersBase::getY);
}

double WithParametersBase::getParameter(int index) const
{
    return (this->*(getters_[index]))();
}

Indeed it works. With a test program:

int main(int argc, char *argv[])
{
   WithParametersBase base;

   std::cout << base.getParameter(0)
             << base.getParameter(1) << std::endl;

   return 0;
}

The printout is correct:

01

But in case I wnat to extend this class:

class WithParametersDerived : public WithParametersBase
{
public:
    WithParametersDerived();
    double getZ() const  {return 2.0;} // A new getter
};

WithParametersDerived::WithParametersDerived()
{
    // I want to integrate the new getter into the previous interface
    addGetter(&WithParametersDerived::getZ); 
}

so that if I call:

WithParametersDerived derived;
std::cout << derived.getParameter(2) << std::endl;

I want to get get a

2

I cannot compile the program. I get an error:

error: no matching function for call to
'WithParametersDerived::addGetter
(double (WithParametersDerived::*)()const)'

Which is reasonable, but I do not know how else to implement it.

I want the creator of the derived class to be able to just add the new getter. I know, that it somehow doesn’t feel right doing all this at runtime, but I do not see a template solution or a preprocessor solution. If you have some suggestions, please let me know. Anything!

  • 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-07T06:18:03+00:00Added an answer on June 7, 2026 at 6:18 am

    I’ll sidestep the why you need such a scheme, and focus on the how.

    Instead of member function pointers, you can use a std::function<double ()>, which is a generic wrapper around any callable entity having the signature double foo(). To create a std::function<double ()> out of a member function and an object instance, you use std::bind as follows:

    std::function<double ()> callback =
        std::bind(&Class::memberFunction, objectInstancePointer);
    

    If you’re not using C++11, std::function and std::bind are also available in Boost as boost::function and boost::bind. The Boost documentation for these are mostly (if not entirely) applicable to their C++11 counterparts.

    Instead of a std::vector, you can use a std::map to index getters by name. This may be more practical than maintaining a central list of parameter ID numbers.

    If your parameters can be of different type than double, then you may want to consider using boost::any or boost::variant as the return type.

    Here’s a complete working example using std::function, std::bind, and std::map:

    #include <cassert>
    #include <map>
    #include <iostream>
    #include <functional>
    
    class WithParametersBase
    {
    public:
        WithParametersBase()
        {
            addGetter("X", std::bind(&WithParametersBase::getX, this));
            addGetter("Y", std::bind(&WithParametersBase::getY, this));
        }
    
        virtual double getX() const {return 0.0;}
        virtual double getY() const {return 1.0;}
    
        // Access parameter by name
        double getParameter(const std::string& name) const
        {
            auto getterIter = getters_.find(name);
            assert(getterIter != getters_.end());
            return getterIter->second();
        }
    
    protected:
        typedef std::function<double ()> ParameterGetter;
        typedef std::map<std::string, ParameterGetter> GetterMap;
    
        void addGetter(const std::string& name, const ParameterGetter& getter)
        {
            getters_[name] = getter;
        }
    
        GetterMap getters_;
    };
    
    class WithParametersDerived : public WithParametersBase
    {
    public:
        WithParametersDerived()
        {
            addGetter("Z", std::bind(&WithParametersDerived::getZ, this));
    
            // Override base class getX
            addGetter("X", std::bind(&WithParametersDerived::getX, this));
        }
    
        double getX() const {return 3.0;} 
        double getZ() const {return 2.0;} // A new getter
    };
    
    int main(int argc, char *argv[])
    {
        WithParametersBase base;
        WithParametersDerived derived;
        WithParametersBase& polymorphic = derived;
    
        std::cout << base.getParameter("X")
                  << base.getParameter("Y")
                  << polymorphic.getParameter("X")
                  << polymorphic.getParameter("Y")
                  << polymorphic.getParameter("Z") << std::endl;
    
        return 0;
    }
    

    The downside of this approach is that each instance of WithParametersBase (or a descendant) will contain a GetterMap. If you have a large amount of such objects, the memory overhead of all those GetterMaps may be undesirable.


    Here’s a more efficient solution that does away with std::function and std::bind. Regular function pointers and static member functions are used for getter callbacks. The object instance for which a parameter is requested is passed as an argument to these static member functions. In derived types, the instance reference is first downcast to the derived type before invoking the member function that does the actual getting.

    There is now only one GetterMap per class instead of per object. Note the use of the “construct on first use” idiom in the getters() method to avoid static initialization order fiasco.

    The downside with this solution is that there is more boilerplate code to write for each class derived from WithParametersBase. It might be possible to reduce the amount of boilerplate code using templates (it would definitely be possible with macros).

    #include <cassert>
    #include <map>
    #include <iostream>
    
    class WithParametersBase
    {
    public:
        virtual double getX() const {return 0.0;}
        virtual double getY() const {return 1.0;}
    
        // Access parameter by name
        double getParameter(const std::string& name) const
        {
            auto getterIter = getters().find(name);
            assert(getterIter != getters().end());
            return getterIter->second(*this);
        }
    
    protected:
        typedef double (*ParameterGetter)(const WithParametersBase& instance);
        typedef std::map<std::string, ParameterGetter> GetterMap;
    
        static double xGetter(const WithParametersBase& instance)
        {
            return instance.getX();
        }
    
        static double yGetter(const WithParametersBase& instance)
        {
            return instance.getY();
        }
    
        static GetterMap makeGetterMap()
        {
            GetterMap map;
            map["X"] = &WithParametersBase::xGetter;
            map["Y"] = &WithParametersBase::yGetter;
            return map;
        }
    
        virtual const GetterMap& getters() const
        {
            // Not thread-safe. Use std::call_once to make thread-safe.
            static GetterMap map = makeGetterMap();
            return map;
        };
    };
    
    class WithParametersDerived : public WithParametersBase
    {
    public:
        double getX() const {return 3.0;} 
        double getZ() const {return 2.0;} // A new getter
    
    protected:
        static double zGetter(const WithParametersBase& instance)
        {
            // It's reasonably safe to assume that 'instance' is of type
            // WithParametersDerived, since WithParametersDerived was the one
            // that associated "Z" with this callback function.
            const WithParametersDerived& derived =
                dynamic_cast<const WithParametersDerived&>(instance);
            return derived.getZ();
        }
    
        static GetterMap makeGetterMap()
        {
            // We "inherit" the getter map from the base class before extending it.
            GetterMap map = WithParametersBase::makeGetterMap();
            map["Z"] = &WithParametersDerived::zGetter;
            return map;
        }
    
        virtual const GetterMap& getters() const
        {
            // Not thread-safe. Use std::call_once to make thread-safe.
            static GetterMap map = makeGetterMap();
            return map;
        };
    };
    
    int main(int argc, char *argv[])
    {
        WithParametersBase base;
        WithParametersDerived derived;
        WithParametersBase& polymorphic = derived;
    
        std::cout << base.getParameter("X")
                  << base.getParameter("Y")
                  << polymorphic.getParameter("X")
                  << polymorphic.getParameter("Y")
                  << polymorphic.getParameter("Z") << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i just wondering is there any performance issue or anything thing wrong if that
just think that when I opened my file then when I want to write
I think that looking at others' code is a good way to learn. I'm
Okay, so I have an assignment to code (using Java,but I don't think that
I'm working on a small roguelike game, and for any object/thing that is not
I think that this problem can be sorted using reflection (a technology which I'm
I think that Visual Studio's biggest let down is the Javascript editor. I have
I think that the zipper is a beautiful idea; it elegantly provides a way
I think that some newer languages like JS can do this natively, but I
I think that is not easy to understand what I need reading title, so

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.