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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:00:02+00:00 2026-06-15T11:00:02+00:00

I want to set the function pointer at runtime. But i’m stuck here. When

  • 0

I want to set the function pointer at runtime. But i’m stuck here. When i use global function or static class member function, everything is ok. but, when the function is ordinary class member functions. i always got compiler errors.
Here is the code:

class A   
{
    int val;
public:
    A() { val = 0; }
    A(int j) { val = j; }

    int aFun(int k) {val -= k; return val; }
};

typedef int (* func)(int );
class B
{
    func m_addr;
public:
    B(func param)
        : m_addr(param)
    {

    }
    void execute()
    {
        cout << m_addr(9) << endl;
    }
};

I’m trying to use them like this:

/* error C2355: ‘this’ : can only be referenced inside non-static
member functions error C2064: term does not evaluate to a function
taking 1 arguments class does not define an ‘operator()’ or a user
defined conversion operator to a pointer-to-function or
reference-to-function that takes appropriate number of arguments
*/

A a;
B b(A::aFun); 
b.execute();

after googled a lot, i found that std::mem_fun may be helpful. but i don’t know how to use it. anyone can help me?

PS: i’m using Visual C++ 2010

  • 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-15T11:00:04+00:00Added an answer on June 15, 2026 at 11:00 am

    Class member functions have an extra parameter passed to it the by the compiler called this, so the compiler sees aFun as begin declared and written as

    int A::aFun(A* this, int k)
    {
        this->val -= k;
        return this->val;
    }
    

    Static/global functions don’t have this parameter and so compilation succeeds.

    In order to use A::aFun you’ll need an instance of class A somewhere.

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

Sidebar

Related Questions

I am trying to use variable in different function, I want to set global
i want set class for repeater when doPostBack for those dataID i wan but
I have a function pointer inside a struct that gets dynamically set at runtime
I want to use magic function __set() and __get() for storing SQL data inside
I want to retrieve property value in jquery function and want to set it
I want to return a set of values from function till the point they
I want to create a function which plot on screen a set of figures
I want to create a postgres function that builds the set of columns it
I want set Listbox background to transparent but not working Is there any idea?
I want to use a DLL in my program which has a function void

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.