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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:40:23+00:00 2026-06-10T07:40:23+00:00

DISCLAIMER I DO NOT USE BOOST OR OTHER LIBRARIES Finally I’ve learned how PointerToMemberFunction

  • 0

DISCLAIMER I DO NOT USE BOOST OR OTHER LIBRARIES

Finally I’ve learned how PointerToMemberFunction works. This is my example code.

#include <iostream>

using namespace std;

class Foo
{
        public:
                void foo ( )
                {
                        cout << "I'm a foo method\n";
                };
};

class Bar
{
        public:
                void bar ( Foo* fooPtr , void(Foo::*fooFnPtr)() )
                {
                        (fooPtr->*fooFnPtr)();
                };
};

int main()
{
        Foo* foo = new Foo();
        Bar* bar = new Bar();

        bar->bar ( foo , &Foo::foo );

        return 0;
}

Now, what the problem is. Bar::bar must be modified somehow, because in real project it won’t know, what class fooFnPtr is a pointer to. In other words Bar::bar must work with any class, not only with Foo. I won’t know, a pointer to an instance of what class is passed to Bar::bar.

The one thing which can help is that all classes which will work with Bar::bar are children of one class!

Is this achievable and how? How do i fix my code? Thanks in advance!

  • 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-10T07:40:25+00:00Added an answer on June 10, 2026 at 7:40 am

    You could make bar a template function:

    template<class T>
    void bar ( T* fooPtr , void(T::*fooFnPtr)() )
    {
        (fooPtr->*fooFnPtr)();
    }
    

    Of course, if you only want to pass pointers to members that exist in the common base class, you can simply do this:

    #include <iostream>
    
    using namespace std;
    
    class Foo
    {
            public:
                    virtual void foo ( )
                    {
                            cout << "I'm a foo method\n";
                    };
    };
    
    class Derived: public Foo
    {
            public:
                    virtual void foo ( )
                    {
                            cout << "I'm a Derived method\n";
                    };
    };
    
    
    class Bar
    {
            public:
                    void bar ( Foo* fooPtr , void(Foo::*fooFnPtr)() )
                    {
                            (fooPtr->*fooFnPtr)();
                    }
    };
    
    int main()
    {
            Derived* derived = new Derived();
            Bar* bar = new Bar();
    
            bar->bar ( derived , &Foo::foo );
    
            return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DISCLAIMER I'm not allowed to use BOOST or any other library, only standard. In
DISCLAIMER: The following code is not something I would ever use in a real
Disclaimer: not sure this is WordPress related or not. I'm following a simple tutorial
Disclaimer: this is not a question about how to install asp.net or an application
Disclaimer: This is for a programming class, but it is not the answer or
DISCLAIMER: This question was not meant to be argumentative! What is fastest and less
Disclaimer: please do not bother reading this long post unless you are an embedded
DISCLAIMER: Don't use ::feof() as your loop condition. For example, see the answer to:
(Firstly, as a disclaimer, this is related to an assignment. I'm not asking anyone
Disclaimer This is not a question about whether we should be escaping for database

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.