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

  • Home
  • SEARCH
  • 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 8600979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:44:21+00:00 2026-06-12T01:44:21+00:00

The question is the following: consider this piece of code: #include <iostream> class aClass

  • 0

The question is the following: consider this piece of code:

#include <iostream>


class aClass
{
public:
    void aTest(int a, int b)
    {
        printf("%d + %d = %d", a, b, a + b);
    }
};

void function1(void (*function)(int, int))
{
    function(1, 1);
}

void test(int a,int b)
{
    printf("%d - %d = %d", a , b , a - b);
}

int main()
{
    aClass a;

    function1(&test);
    function1(&aClass::aTest); // <-- How should I point to a's aClass::test function?
}

How can I use the a‘s aClass::test as an argument to function1? I would like to access a member of the class.

  • 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-12T01:44:22+00:00Added an answer on June 12, 2026 at 1:44 am

    There isn’t anything wrong with using function pointers. However, pointers to non-static member functions are not like normal function pointers: member functions need to be called on an object which is passed as an implicit argument to the function. The signature of your member function above is, thus

    void (aClass::*)(int, int)
    

    rather than the type you try to use

    void (*)(int, int)
    

    One approach could consist in making the member function static in which case it doesn’t require any object to be called on and you can use it with the type void (*)(int, int).

    If you need to access any non-static member of your class and you need to stick with function pointers, e.g., because the function is part of a C interface, your best option is to always pass a void* to your function taking function pointers and call your member through a forwarding function which obtains an object from the void* and then calls the member function.

    In a proper C++ interface you might want to have a look at having your function take templated argument for function objects to use arbitrary class types. If using a templated interface is undesirable you should use something like std::function<void(int, int)>: you can create a suitably callable function object for these, e.g., using std::bind().

    The type-safe approaches using a template argument for the class type or a suitable std::function<...> are preferable than using a void* interface as they remove the potential for errors due to a cast to the wrong type.

    To clarify how to use a function pointer to call a member function, here is an example:

    // the function using the function pointers:
    void somefunction(void (*fptr)(void*, int, int), void* context) {
        fptr(context, 17, 42);
    }
    
    void non_member(void*, int i0, int i1) {
        std::cout << "I don't need any context! i0=" << i0 << " i1=" << i1 << "\n";
    }
    
    struct foo {
        void member(int i0, int i1) {
            std::cout << "member function: this=" << this << " i0=" << i0 << " i1=" << i1 << "\n";
        }
    };
    
    void forwarder(void* context, int i0, int i1) {
        static_cast<foo*>(context)->member(i0, i1);
    }
    
    int main() {
        somefunction(&non_member, nullptr);
        foo object;
        somefunction(&forwarder, &object);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have faced the following interview question. Consider this function declaration: void quiz(int i)
sorry if this is a newB question, please conider the following code: #include <boost/multi_index_container.hpp>
Java noob question: Consider the following C array and initializer code: struct { int
Consider the following piece of code: final Foo foo = context.mock(Foo.class); context.checking(new Expectations() {{
To illustrate my question consider the following example: @Entity public class Box implements Serializable
I'm confused about an aspect of polymorphism. Please consider the following code: #include <iostream>
Consider following piece of code void foo( bool forwad ) { vector<MyObject>::iterator it, end_it;
Consider the following code: #include<stdio.h> #define k 0.7 #define p 0.5 int main() {
This is an even more in-depth follow-on to: this question Consider the following code:
This is a very basic question, so please bear with me. Consider the following

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.