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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:26:34+00:00 2026-05-28T04:26:34+00:00

I wanted to pass a function object to a class, and the class will

  • 0

I wanted to pass a function object to a class, and the class will use the function object to do some job inside the class.

But the problem is that, I don’t what the function object will be passed in. So I figured that, define a void * pointer in the class, this pointer will be initialized with the function object which will be passed in.

Code goes below:

class A 
{
    public:
        //...
        void doSomeJob(int x)
        {
            (*functor)(x); //use the function object to process data x
        }

    private:
        //...
        void *functor; //function object's pointer will be assigned to this pointer
};

But the code doesn’t work. I guess, can’t use void *functor that way.

I know I can use template class to finish the job, but my question is, can I still do the job using pointer to function object and how?

PS

To make my problem clearer, there may be several function objects which differ from each other by how they process data, I don’t what function object will be passed in, but I do know each of them will take a int parameter.

Just as some answers tell, I can do the job through function pointer, but function object has more utilities than function pointers, such as states, and that’s what I’m gonna use.

  • 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-05-28T04:26:35+00:00Added an answer on May 28, 2026 at 4:26 am

    You can’t call a function object of a type unknown to you at the call site if its type is not stored somewhere accessible to the call machinery.

    There are two options:

    If you can use C++11 or boost, you can use std::function resp. boost::function:

    class A
    {
    public:
      // ...
      void doSomeJob(int x)
      {
        functor(x);
      }
    private:
      std::function<void(int)> functor; // or boost::function when using boost
    };
    

    Here the type is stored (in an implicit form) inside the mechanism of the function template.

    Otherwise, if you can require that all function objects passed have a class type derived from a specific base class, you can create an abstract base class:

    struct AbstractFunctor
    {
      virtual void operator()(int) = 0;
    };
    
    class A
    {
    public:
      // ...
      void doSomeJob(int x)
      {
        (*functor)(x);
      }
    private:
      AbstractFunctor* functor; // or boost::function when using boost
    };
    

    Here the type is stored in the virtual table of the function object.

    If you really can’t use boost, you also might write a similar solution yourself. The key word is “type erasure”, and it basically works by generating on the fly a derived object from a known base class (as in my second solution) which knows about your object’s type and can call it. It might be done roughly as follows (untested code):

    class int_function
    {
    private:
      struct abstract_forward
      {
        virtual void call(int) = 0;
        virtual abstract_forward clone() const = 0;
        virtual ~abstract_forward() {}
      };
      template<typename Functor> struct forward: abstract_forward
      {
        forward(Functor f): func(f) {}
        void call(int i) { func(i); }
        abstract_forward clone() const { return new forward<Functor>(func); }
        Functor func;
      };
    public:
      template<typename Functor> int_function(Functor f)
      {
        forwarder = new forward<Functor>(f);
      }
      int_function(int_function const& other)
      {
        forwarder = other.forwarder->clone();
      }
      int_function& operator=(int_function const& other)
      {
        abstract_forward* newfwd = other.forwarder->clone();
        delete forwarder;
        forwarder = newfwd;
      }
      ~int_function()
      {
        delete forwarder}
      }
      void operator()(int i)
      {
        forwarder->call(i);
      }
    private:
      abstract_forward* forwarder;
    };
    
    class A
    {
    public:
      void doSomeJob(int x)
      {
        functor(x);
      }
    private:
      int_function functor;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Language: C++, MFC Problem: I am attempting to pass a function some pointers to
Up until now, whenever I wanted to pass some raw data to a function
I wanted some of those spiffy rounded corners for a web project that I'm
I'm wanted to convert some of my python code to C++ for speed but
I have a program in python that includes a class that takes a function
In class B below I wanted the __set__ function in class A to be
I'm trying to create a function that will store and repeat another function given
I wanted to implement my own debug function that has the same signature as
I wanted to add a function to my Slim application but I'm not familiar
Wanted to get some consensus around a UI feature I'm working on right now.

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.