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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:04:00+00:00 2026-06-18T03:04:00+00:00

Is it possible in C++? For example I have a pointer to a function

  • 0

Is it possible in C++?
For example I have a pointer to a function that takes no parameters and its return type is void:

void (*f)();

and and a function object:

class A
{
public:
    void operator()() { cout << "functor\n"; }
};

Is it possible to assign to f the address of an A object? And when I call f() to call the A functor?

I tried this but it doesn’t work:

#include <iostream>
using namespace std;

class A
{
public:
    void operator()() { cout << "functorA\n"; }
};

int main()
{
    A ob;
    ob();
    void (*f)();
    f = &ob;
    f();       // Call ob();
    return 0;
}

I get C:\Users\iuliuh\QtTests\functor_test\main.cpp:15: error: C2440: '=' : cannot convert from 'A *' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible

Is there any way to achieve this?

  • 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-18T03:04:02+00:00Added an answer on June 18, 2026 at 3:04 am

    You can’t do it in the way you’ve specified, because:

    1. operator() must be a nonstatic function (standards requirement)
    2. a pointer to a non-static function must have an implicit parameter – the pointer to the class instance
    3. your call to f() does not give any indication on which instance of the object A your function is called

    Using C++11 and std::function, as Stephane Rolland pointed out, may do the trick – you’ll be specifying the pointer to the object in the binding:

    std::function<void(void)> f = std::bind(&A::operator(), &ob);
    

    (See question on using std::function on member functions)

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

Sidebar

Related Questions

I have a struct that takes a function pointer, like this: typedef int (*node_transition_func)(
I have some code within one function that I want to separate into its
I've got a function inside a class (A) that essentially takes as a parameter
I have a function that takes, as an argument, an stl vector of pointers
I have a pointer to a COM object that implements an undocumented interface. I
Is it possible to define operators for enums? For example I have enum Month
Is intercontainer communication possible within Apache tomcat? For example: Say you have 2 rest
Is it possible to extend multiple classes in Scala. For example if I have
Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: Check synchronously if file/directory exists in Node.js For example, i have a

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.