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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:30:57+00:00 2026-06-01T04:30:57+00:00

I suspect this is impossible, but thought I’d ask. Say I have a class

  • 0

I suspect this is impossible, but thought I’d ask. Say I have a class with a method:

class A {
public:
    void b(int c);
};

I can make a pointer to that member function:

void (A::*ptr)(int) = &A::b;
(someAInstance.*ptr)(123);

I can also abuse function pointers and make a pointer that takes the A argument directly (I don’t know if this is safe, but it works on my machine):

void (*ptr2)(A*, int) = (void (*)(A*, int))&A::b;
(*ptr2)(&someAInstance, 123);

What I want is to somehow curry the A argument, and create a function pointer that just takes an int, but calls the A::b method on a particular A instance I’ve predefined. The A instance will stay constant for that particular function pointer, but there may be several function pointers all pointing to the same A::b method, but using different A instances. For example, I could make a separate wrapper function:

A* someConstantA = new A;
void wrapper(int c) {
    someConstantA->b(c);
}

void (*ptr3)(int) = &wrapper;

Now I can use ptr3 without knowing which particular A it’s dispatching the call to, but I had to define a special function to handle it. I need a way to make pointers for any number of A instances, so I can’t hardcode it like that. Is this in any way possible?


Edit: Should’ve mentioned, I’m trapped in C++03 land, and also can’t use Boost

  • 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-01T04:30:59+00:00Added an answer on June 1, 2026 at 4:30 am

    If you have a sufficiently new compiler (e.g. gcc 4.2+), it should include TR1, where you could use std::tr1::bind:

    #include <cstdio>
    #include <tr1/functional>
    
    class A {
    public:
        void b(int c) {
            printf("%p, %d\n", (void*)this, c);
        }
    };
    
    int main() {
        A* a = new A;
    
        std::tr1::function<void(int)> f =
            std::tr1::bind(&A::b, a, std::tr1::placeholders::_1);  // <--
        f(4);
    
        delete a;
    
        return 0;
    }
    

    It is also doable in pure C++03 without TR1, but also much more messier:

    std::binder1st<std::mem_fun1_t<void, A, int> > f =
        std::bind1st(std::mem_fun(&A::b), a);
    

    You could also write your own function objects.

    Note that, in all the above cases, you need to be very careful about the lifetime of a since that is a bare pointer. With std::tr1::bind, you could at least wrap the pointer in a std::tr1::shared_ptr, so that it can live just as long as the function object.

    std::tr1::shared_ptr<A> a (new A);
    std::tr1::function<void(int)> f =
        std::tr1::bind(&A::b, a, std::tr1::placeholders::_1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I suspect this isn't possible as the anonymous inner class is private. Can I
I suspect that this is impossible, but I'm trying to be optimistic... I'm running
After re-reading the CSS2.1 and CSS3 selector specs I suspect this is impossible, but
I suspect this has been asked before, but can't seem to find a question
I suspect this is not possible under the current C++ standards but I'll ask
I suspect this has already been answered somewhere, but I can't find it, so...
I suspect this applies to general ASP.Net too but I am not sure. If
One day I suspect I'll have to learn hadoop and transfer all this data
(This might be better in the TestComplete forums, but I thought I'd give it
I suspected this wouldn't work, and it didn't - but I can't seem to

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.